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实现的一个简单LRU cache
Sep 26 Python
python实现上传样本到virustotal并查询扫描信息的方法
Oct 05 Python
python将图片文件转换成base64编码的方法
Mar 14 Python
教你使用python画一朵花送女朋友
Mar 29 Python
详解Python3的TFTP文件传输
Jun 26 Python
有关Python的22个编程技巧
Aug 29 Python
Python中整数的缓存机制讲解
Feb 16 Python
python web框架中实现原生分页
Sep 08 Python
python做接口测试的必要性
Nov 20 Python
Python带参数的装饰器运行原理解析
Jun 09 Python
Linux安装Python3如何和系统自带的Python2并存
Jul 23 Python
Python绘制分类图的方法
Apr 20 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
DC的38部超级英雄动画电影
2020/03/03 欧美动漫
经典的星际争霸,满是回忆的BGM
2020/04/09 星际争霸
PHP的变量类型和作用域详解
2014/03/12 PHP
PHP实现算式验证码和汉字验证码实例
2015/03/09 PHP
PHP使用preg_split()分割特殊字符(元字符等)的方法分析
2017/02/04 PHP
在网页中屏蔽快捷键
2006/09/06 Javascript
javascript和HTML5利用canvas构建猜牌游戏实现算法
2013/07/17 Javascript
根据当前时间在jsp页面上显示上午或下午
2014/08/18 Javascript
jQuery通用的全局遍历方法$.each()用法实例
2016/07/04 Javascript
相册展示PhotoSwipe.js插件实现
2016/08/25 Javascript
AngularJS 模块化详解及实例代码
2016/09/14 Javascript
JS正则表达式学习之贪婪和非贪婪模式实例总结
2016/12/26 Javascript
JavaScript正则获取地址栏中参数的方法
2017/03/02 Javascript
80%应聘者都不及格的JS面试题
2017/03/21 Javascript
浅谈webpack下的AOP式无侵入注入
2017/11/12 Javascript
vue项目首屏加载时间优化实战
2019/04/23 Javascript
使用 Vue-TCB 快速在 Vue 应用中接入云开发的方法
2020/02/10 Javascript
JS简易计算器实例讲解
2020/06/30 Javascript
浅谈vue生命周期共有几个阶段?分别是什么?
2020/08/07 Javascript
[原创]python爬虫(入门教程、视频教程)
2018/01/08 Python
django中的HTML控件及参数传递方法
2018/03/20 Python
python自动发送邮件脚本
2018/06/20 Python
Python运行DLL文件的方法
2020/01/17 Python
html5 Web SQL Database 之事务处理函数transaction与executeSQL解析
2013/11/07 HTML / CSS
用HTML5实现鼠标滚轮事件放大缩小图片的功能
2015/06/25 HTML / CSS
瑞典度假品牌:OAS
2019/05/28 全球购物
美国最大的烧烤架和户外生活用品专业零售商:Barbeques Galore
2021/01/09 全球购物
缴纳养老保险的证明
2014/01/10 职场文书
缓刑人员的思想汇报
2014/01/11 职场文书
阳光体育活动方案
2014/02/16 职场文书
委托书样本
2014/04/02 职场文书
教师党员个人自我剖析材料
2014/09/29 职场文书
见习期个人总结
2015/03/05 职场文书
2015员工年度考核评语
2015/03/25 职场文书
房地产财务经理岗位职责
2015/04/08 职场文书
go goth封装第三方认证库示例详解
2022/08/14 Golang