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 strip lstrip rstrip使用方法
Sep 06 Python
python定时检查启动某个exe程序适合检测exe是否挂了
Jan 21 Python
Python3.6实现带有简单界面的有道翻译小程序
Apr 16 Python
python实践项目之监控当前联网状态详情
May 23 Python
基于python的列表list和集合set操作
Nov 24 Python
python使用itchat模块给心爱的人每天发天气预报
Nov 25 Python
python mysql 字段与关键字冲突的解决方式
Mar 02 Python
Django 自定义权限管理系统详解(通过中间件认证)
Mar 11 Python
Python描述符descriptor使用原理解析
Mar 21 Python
python爬虫智能翻页批量下载文件的实例详解
Feb 02 Python
OpenCV-Python实现图像平滑处理操作
Jun 08 Python
Python 文字识别
May 11 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
NOT NULL 和NULL
2007/01/15 PHP
php抽奖小程序的实现代码
2013/06/18 PHP
php封装json通信接口详解及实例
2017/03/07 PHP
Yii2第三方类库插件Imagine的安装和使用
2017/07/06 PHP
PHP面向对象程序设计__tostring()和__invoke()用法分析
2019/06/12 PHP
Prototype使用指南之string.js
2007/01/10 Javascript
一个网马的tips实现分析
2010/11/28 Javascript
基于jQuery的一个扩展form序列化到json对象
2010/12/09 Javascript
javascript实现TreeView 无刷新展开的实例代码
2013/07/13 Javascript
javascript的parseFloat()方法精度问题探讨
2013/11/26 Javascript
js Dialog 去掉右上角的X关闭功能
2014/04/23 Javascript
javascript中定义类的方法汇总
2014/12/28 Javascript
Nodejs中读取中文文件编码问题、发送邮件和定时任务实例
2015/01/01 NodeJs
JS实现密码框根据焦点的获取与失去控制文字的消失与显示效果
2015/11/26 Javascript
jQuery代码实现图片墙自动+手动淡入淡出切换效果
2016/05/09 Javascript
jQuery原理系列-css选择器的简单实现
2016/06/07 Javascript
轻松实现jquery选项卡切换效果
2016/10/10 Javascript
jQuery代码实现实时获取时间
2017/01/29 Javascript
利用JS动态生成隔行换色HTML表格的两种方法
2018/10/09 Javascript
[02:37]TI8勇士令状不朽珍藏II视频展示
2018/06/23 DOTA
python+django快速实现文件上传
2016/10/24 Python
python笔记_将循环内容在一行输出的方法
2019/08/08 Python
python next()和iter()函数原理解析
2020/02/07 Python
Python astype(np.float)函数使用方法解析
2020/06/08 Python
Django nginx配置实现过程详解
2020/09/10 Python
python 牛顿法实现逻辑回归(Logistic Regression)
2020/10/15 Python
分享8款纯CSS3实现的搜索框功能
2017/09/14 HTML / CSS
浅析canvas元素的html尺寸和css尺寸对元素视觉的影响
2019/07/22 HTML / CSS
一级方程式赛车官方网上商店:F1 Store(支持中文)
2018/01/12 全球购物
计算 s=(x*y)1/2,用两个宏定义来实现
2016/08/11 面试题
工会经费申请报告
2015/05/15 职场文书
活动费用申请报告
2015/05/15 职场文书
2015大学生暑假调查报告
2015/07/13 职场文书
团队合作精神学习心得体会
2016/01/19 职场文书
如何使用vue3打造一个物料库
2021/05/08 Vue.js
javascript函数式编程基础
2021/09/15 Javascript