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在校内发人人网状态(人人网看状态)
Feb 19 Python
python实现端口转发器的方法
Mar 13 Python
Python+Opencv识别两张相似图片
Mar 23 Python
python爬取淘宝商品详情页数据
Feb 23 Python
pytorch构建网络模型的4种方法
Apr 13 Python
Python开发的十个小贴士和技巧及长常犯错误
Sep 27 Python
django session完成状态保持的方法
Nov 27 Python
python-opencv获取二值图像轮廓及中心点坐标的代码
Aug 27 Python
如何在mac环境中用python处理protobuf
Dec 25 Python
Anaconda+Pycharm环境下的PyTorch配置方法
Mar 13 Python
django 读取图片到页面实例
Mar 27 Python
matplotlib部件之套索Lasso的使用
Feb 24 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入门之连接mysql数据库的一个类
2012/04/21 PHP
php将session放入memcached的设置方法
2014/02/14 PHP
PHP实现登陆表单提交CSRF及验证码
2017/01/24 PHP
用javascript实现分割提取页面所需内容
2007/05/09 Javascript
PHP 与 js的通信(via ajax,json)
2010/11/16 Javascript
javascript阻止scroll事件多次执行的思路及实现
2013/11/08 Javascript
关于Javascript 对象(object)的prototype
2014/05/09 Javascript
href下载文件根据id取url并下载
2014/05/28 Javascript
js防止DIV布局滚动时闪动的解决方法
2014/10/30 Javascript
JavaScript实现强制重定向至HTTPS页面
2015/06/10 Javascript
EditPlus中的正则表达式 实战(4)
2016/12/15 Javascript
React Router基础使用
2017/01/17 Javascript
浅谈函数调用的不同方式,以及this的指向
2017/09/17 Javascript
详解vue静态资源打包中的坑与解决方案
2018/02/05 Javascript
vue实现模态框的通用写法推荐
2018/02/26 Javascript
vue 监听键盘回车事件详解 @keyup.enter || @keyup.enter.native
2018/08/25 Javascript
详解vue 数组和对象渲染问题
2018/09/21 Javascript
Vue-drag-resize 拖拽缩放插件的使用(简单示例)
2019/12/04 Javascript
用python实现的可以拷贝或剪切一个文件列表中的所有文件
2009/04/30 Python
使用Python编写类UNIX系统的命令行工具的教程
2015/04/15 Python
Python正则表达式匹配中文用法示例
2017/01/17 Python
基于python中的TCP及UDP(详解)
2017/11/06 Python
Python编程实现二分法和牛顿迭代法求平方根代码
2017/12/04 Python
opencv改变imshow窗口大小,窗口位置的方法
2018/04/02 Python
pycharm设置注释颜色的方法
2018/05/23 Python
Python学习笔记之变量、自定义函数用法示例
2019/05/28 Python
解决python文件双击运行秒退的问题
2019/06/24 Python
Python的Lambda函数用法详解
2019/09/03 Python
python海龟绘图之画国旗实例代码
2020/11/11 Python
德国购买健身器材:AsVIVA
2017/08/09 全球购物
莫斯科绝对前卫最秘密的商店:SVMoscow
2017/10/23 全球购物
求职简历自荐信范文
2013/10/21 职场文书
初级职称评定工作总结
2015/08/13 职场文书
庆祝教师节主题班会
2015/08/17 职场文书
python文件与路径操作神器 pathlib
2022/04/01 Python
MySQL数据库 任意ip连接方法
2022/05/20 MySQL