Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果示例


Posted in Python onJanuary 29, 2018

本文实例讲述了Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果。分享给大家供大家参考,具体如下:

这里用 Python 实现 PS 滤镜特效,Marble Filter, 这种滤镜使图像产生不规则的扭曲,看起来像某种玻璃条纹, 具体的代码如下:

import numpy as np
import math
import numpy.matlib
from skimage import io
import random
from skimage import img_as_float
import matplotlib.pyplot as plt
def Init_arr():
  B = 256
  P = np.zeros((B+B+2, 1))
  g1 = np.zeros((B+B+2, 1))
  g2 = np.zeros((B+B+2, 2))
  g3 = np.zeros((B+B+2, 3))
  N_max = 1e6
  for i in range(B+1):
    P[i] = i
    g1[i] = (((math.floor(random.random()*N_max)) % (2*B))-B)*1.0/B
    g2[i, :] = (np.mod((np.floor(np.random.rand(1, 2)*N_max)), (2*B))-B)*1.0/B
    g2[i, :] = g2[i, :] / np.sum(g2[i, :] **2)
    g3[i, :] = (np.mod((np.floor(np.random.rand(1, 3)*N_max)), (2*B))-B)*1.0/B
    g3[i, :] = g3[i, :] / np.sum(g3[i, :] **2)
  for i in range(B, -1, -1):
    k = P[i]
    j = math.floor(random.random()*N_max) % B
    P [i] = P [j]
    P [j] = k
  P[B+1:2*B+2]=P[0:B+1];
  g1[B+1:2*B+2]=g1[0:B+1];
  g2[B+1:2*B+2, :]=g2[0:B+1, :]
  g3[B+1:2*B+2, :]=g3[0:B+1, :]
  P = P.astype(int)
  return P, g1, g2, g3
def Noise_2(x_val, y_val, P, g2):
  BM=255
  N=4096
  t = x_val + N
  bx0 = ((np.floor(t).astype(int)) & BM) + 1
  bx1 = ((bx0 + 1).astype(int) & BM) + 1
  rx0 = t - np.floor(t)
  rx1 = rx0 - 1.0
  t = y_val + N
  by0 = ((np.floor(t).astype(int)) & BM) + 1
  by1 = ((bx0 + 1).astype(int) & BM) + 1
  ry0 = t - np.floor(t)
  ry1 = rx0 - 1.0
  sx = rx0 * rx0 * (3 - 2.0 * rx0)
  sy = ry0 * ry0 * (3 - 2.0 * ry0)
  row, col = x_val.shape
  q1 = np.zeros((row, col ,2))
  q2 = q1.copy()
  q3 = q1.copy()
  q4 = q1.copy()
  for i in range(row):
    for j in range(col):
      ind_i = P[bx0[i, j]]
      ind_j = P[bx1[i, j]]
      b00 = P[ind_i + by0[i, j]]
      b01 = P[ind_i + by1[i, j]]
      b10 = P[ind_j + by0[i, j]]
      b11 = P[ind_j + by1[i, j]]
      q1[i, j, :] = g2[b00, :]
      q2[i, j, :] = g2[b10, :]
      q3[i, j, :] = g2[b01, :]
      q4[i, j, :] = g2[b11, :]
  u1 = rx0 * q1[:, :, 0] + ry0 * q1[:, :, 1]
  v1 = rx1 * q2[:, :, 0] + ry1 * q2[:, :, 1]
  a = u1 + sx * (v1 - u1)
  u2 = rx0 * q3[:, :, 0] + ry0 * q3[:, :, 1]
  v2 = rx1 * q4[:, :, 0] + ry1 * q4[:, :, 1]
  b = u2 + sx * (v2 - u2)
  out = (a + sy * (b - a)) * 1.5
  return out
file_name='D:/Visual Effects/PS Algorithm/4.jpg';
img=io.imread(file_name)
img = img_as_float(img)
row, col, channel = img.shape
xScale = 25.0
yScale = 25.0
turbulence =0.25
xx = np.arange (col)
yy = np.arange (row)
x_mask = numpy.matlib.repmat (xx, row, 1)
y_mask = numpy.matlib.repmat (yy, col, 1)
y_mask = np.transpose(y_mask)
x_val = x_mask / xScale
y_val = y_mask / yScale
Index = np.arange(256)
sin_T=-yScale*np.sin(2*math.pi*(Index)/255*turbulence);
cos_T=xScale*np.cos(2*math.pi*(Index)/255*turbulence)
P, g1, g2, g3 = Init_arr()
Noise_out = Noise_2(x_val, y_val, P, g2)
Noise_out = 127 * (Noise_out + 1)
Dis = np.floor(Noise_out)
Dis[Dis>255] = 255
Dis[Dis<0] = 0
Dis = Dis.astype(int)
img_out = img.copy()
for ii in range(row):
  for jj in range(col):
    new_x = jj + sin_T[Dis[ii, jj]]
    new_y = ii + cos_T[Dis[ii, jj]]
    if (new_x > 0 and new_x < col-1 and new_y > 0 and new_y < row-1):
      int_x = int(new_x)
      int_y = int(new_y)
      img_out[ii, jj, :] = img[int_y, int_x, :]
plt.figure(1)
plt.title('3water.com')
plt.imshow(img)
plt.axis('off');
plt.figure(2)
plt.title('3water.com')
plt.imshow(img_out)
plt.axis('off');
plt.show();

运行效果:

Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果示例

Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果示例

Python 相关文章推荐
使用Python中的greenlet包实现并发编程的入门教程
Apr 16 Python
Python使用multiprocessing创建进程的方法
Jun 04 Python
Python for Informatics 第11章之正则表达式(四)
Apr 21 Python
pandas多级分组实现排序的方法
Apr 20 Python
python实现时间o(1)的最小栈的实例代码
Jul 23 Python
详解Django中类视图使用装饰器的方式
Aug 12 Python
python中的单引号双引号区别知识点总结
Jun 23 Python
python pytest进阶之fixture详解
Jun 27 Python
Python如何实现强制数据类型转换
Nov 22 Python
numpy按列连接两个维数不同的数组方式
Dec 06 Python
python通过matplotlib生成复合饼图
Feb 06 Python
Python 实现Image和Ndarray互相转换
Feb 19 Python
Python实现识别手写数字大纲
Jan 29 #Python
django文档学习之applications使用详解
Jan 29 #Python
Python实现PS滤镜Fish lens图像扭曲效果示例
Jan 29 #Python
python实现识别手写数字 python图像识别算法
Mar 23 #Python
Python实现简易版的Web服务器(推荐)
Jan 29 #Python
python实现图像识别功能
Jan 29 #Python
Python使用正则表达式获取网页中所需要的信息
Jan 29 #Python
You might like
php查询whois信息的方法
2015/06/08 PHP
php设计模式之组合模式实例详解【星际争霸游戏案例】
2020/03/27 PHP
Windows Live的@live.com域名注册漏洞 利用代码
2006/12/27 Javascript
超清晰的document对象详解
2007/02/27 Javascript
jquery 插件学习(四)
2012/08/06 Javascript
简单时间提示DEMO从0开始一直进行计时
2013/11/19 Javascript
JavaScript中的console.profile()函数详细介绍
2014/12/29 Javascript
javascript结合fileReader 实现上传图片
2015/01/30 Javascript
关注jquery技巧提高jquery技能(前端开发必学)
2015/11/02 Javascript
Google 地图事件实例讲解
2016/08/06 Javascript
Jquery Easyui分割按钮组件SplitButton使用详解(17)
2016/12/18 Javascript
详解vue 中使用 AJAX获取数据的方法
2017/01/18 Javascript
seajs中模块依赖的加载处理实例分析
2017/10/10 Javascript
微信小程序之swiper轮播图中的图片自适应高度的方法
2018/04/23 Javascript
vue better scroll 无法滚动的解决方法
2018/06/07 Javascript
在vue项目中引入vue-beauty操作方法
2019/02/11 Javascript
jdk1.8+vue elementui实现多级菜单功能
2020/09/24 Javascript
Python xlrd读取excel日期类型的2种方法
2015/04/28 Python
python如何通过protobuf实现rpc
2016/03/06 Python
利用Python将时间或时间间隔转为ISO 8601格式方法示例
2017/09/05 Python
Django使用Mysql数据库已经存在的数据表方法
2018/05/27 Python
python实现监控某个服务 服务崩溃即发送邮件报告
2018/06/21 Python
python中plot实现即时数据动态显示方法
2018/06/22 Python
python制作mysql数据迁移脚本
2019/01/01 Python
简单了解python数组的基本操作
2019/11/26 Python
Python使用socket模块实现简单tcp通信
2020/08/18 Python
python实现图片,视频人脸识别(opencv版)
2020/11/18 Python
狗狗玩具、零食和咀嚼物的月度送货服务:Super Chewer
2018/08/22 全球购物
阿里巴巴英国:Alibaba英国
2019/12/11 全球购物
alice McCALL官网:澳大利亚时尚品牌
2020/11/16 全球购物
公司员工检讨书
2014/02/08 职场文书
2015年评职称工作总结范文
2015/04/20 职场文书
优秀教师主要事迹材料
2015/11/04 职场文书
Python自然语言处理之切分算法详解
2021/04/25 Python
零基础学java之带返回值的方法的定义和调用
2022/04/10 Java/Android
Mysql中常用的join连接方式
2022/05/11 MySQL