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中MYSQLdb出现乱码的解决方法
Oct 11 Python
python 调用c语言函数的方法
Sep 29 Python
Queue 实现生产者消费者模型(实例讲解)
Nov 13 Python
Django 生成登陆验证码代码分享
Dec 12 Python
Python unittest模块用法实例分析
May 25 Python
PySide和PyQt加载ui文件的两种方法
Feb 27 Python
Python实现Mysql数据统计及numpy统计函数
Jul 15 Python
django多个APP的urls设置方法(views重复问题解决)
Jul 19 Python
关于python中plt.hist参数的使用详解
Nov 28 Python
sklearn+python:线性回归案例
Feb 24 Python
详解基于Jupyter notebooks采用sklearn库实现多元回归方程编程
Mar 25 Python
Python sklearn中的.fit与.predict的用法说明
Jun 28 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 日期时间函数的高级应用技巧
2009/10/10 PHP
一个完整的php文件上传类实例讲解
2015/10/27 PHP
基于PHP实现等比压缩图片大小
2016/03/04 PHP
php实现的网页版剪刀石头布游戏示例
2016/11/25 PHP
php生成图片缩略图功能示例
2017/02/22 PHP
PHP使用微信开发模式实现搜索已发送图文及匹配关键字回复的方法
2017/09/13 PHP
Js日期选择器并自动加入到输入框中示例代码
2013/08/02 Javascript
JavaScript DOM节点添加示例
2014/07/16 Javascript
innerHTML中标签可以换行的方法汇总
2015/08/14 Javascript
jquery遍历函数siblings()用法实例
2015/12/24 Javascript
简单讲解AngularJS的Routing路由的定义与使用
2016/03/05 Javascript
JavaScript中instanceof运算符的使用示例
2016/06/08 Javascript
javascript中sort排序实例详解
2016/07/24 Javascript
AngularJS教程之MVC体系结构详解
2016/08/16 Javascript
jQuery实现弹出带遮罩层的居中浮动窗口效果
2016/09/12 Javascript
原生js实现键盘控制div移动且解决停顿问题
2016/12/05 Javascript
React教程之Props验证的具体用法(Props Validation)
2017/09/04 Javascript
Angular 容器部署的方法
2018/04/17 Javascript
vscode 开发Vue项目的方法步骤
2018/11/25 Javascript
layer.alert回调函数执行关闭弹窗的实例
2019/09/11 Javascript
JS插件amCharts实现绘制柱形图默认显示数值功能示例
2019/11/26 Javascript
JS document文档的简单操作完整示例
2020/01/13 Javascript
js根据后缀判断文件文件类型的代码
2020/05/09 Javascript
原生js实现五子棋游戏
2020/05/28 Javascript
[06:57]DOTA2-DPC中国联赛 正赛 Ehome vs PSG.LGD 选手采访
2021/03/11 DOTA
Ubuntu 16.04 LTS中源码安装Python 3.6.0的方法教程
2016/12/27 Python
python编程嵌套函数实例代码
2018/02/11 Python
Python闭包执行时值的传递方式实例分析
2018/06/04 Python
利用Python半自动化生成Nessus报告的方法
2019/03/19 Python
python中设置超时跳过,超时退出的方式
2019/12/13 Python
PyTorch中反卷积的用法详解
2019/12/30 Python
python对接ihuyi实现短信验证码发送
2020/05/10 Python
利用css3实现的简单的鼠标悬停按钮
2014/11/04 HTML / CSS
房产委托公证书样本
2014/04/04 职场文书
学校安全管理责任书
2014/07/23 职场文书
在职证明书模板
2015/06/15 职场文书