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静态方法实例
Jan 14 Python
使用Python的Flask框架实现视频的流媒体传输
Mar 31 Python
使用Python的PEAK来适配协议的教程
Apr 14 Python
Python 专题一 函数的基础知识
Mar 16 Python
Python3之文件读写操作的实例讲解
Jan 23 Python
Django框架教程之正则表达式URL误区详解
Jan 28 Python
python 列表,数组和矩阵sum的用法及区别介绍
Jun 28 Python
python爬取百度贴吧前1000页内容(requests库面向对象思想实现)
Aug 10 Python
python实现的接收邮件功能示例【基于网易POP3服务器】
Sep 11 Python
Python进程池Pool应用实例分析
Nov 27 Python
Python3和pyqt5实现控件数据动态显示方式
Dec 13 Python
python 常见的反爬虫策略
Sep 27 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调用数据库的存贮过程
2006/10/09 PHP
PHP中删除变量时unset()和null的区别分析
2011/01/27 PHP
Uncaught exception com_exception with message Failed to create COM object
2012/01/11 PHP
php密码生成类实例
2014/09/24 PHP
PHP 代码简洁之道(小结)
2019/10/16 PHP
Laravel 5.2 文档 数据库 ―― 起步介绍
2019/10/21 PHP
PHP 对象继承原理与简单用法示例
2020/04/21 PHP
document对象execCommand的command参数介绍
2006/08/01 Javascript
深入理解javascript学习笔记(一) 编写高质量代码
2012/08/09 Javascript
JS防止用户多次提交的简单代码
2013/08/01 Javascript
js控制frameSet示例
2013/09/10 Javascript
toggle()隐藏问题的解决方法
2014/02/17 Javascript
对new functionName()定义一个函数的理解
2014/05/22 Javascript
js构造函数、索引数组和属性的实现方式和使用
2014/11/16 Javascript
JavaScript实现同一页面内两个表单互相传值的方法
2015/08/12 Javascript
Javascript函数式编程简单介绍
2015/10/11 Javascript
Javascript的无new构建实例详解
2016/05/15 Javascript
jQuery中Find选择器用法示例
2016/09/21 Javascript
NodeJS仿WebApi路由示例
2017/02/28 NodeJs
Node.js利用Express实现用户注册登陆功能(推荐)
2020/10/26 Javascript
python3.4用函数操作mysql5.7数据库
2017/06/23 Python
Windows下安装Django框架的方法简明教程
2018/03/28 Python
利用python numpy+matplotlib绘制股票k线图的方法
2019/06/26 Python
Django中多种重定向方法使用详解
2019/07/17 Python
HTML5新特性之用SVG绘制微信logo
2016/02/03 HTML / CSS
Linux如何为某个操作添加别名
2013/03/01 面试题
房地产广告词大全
2014/03/19 职场文书
促销活动计划书
2014/05/02 职场文书
2014年酒店服务员工作总结
2014/12/08 职场文书
个人学习总结范文
2015/02/15 职场文书
清洁工工作总结
2015/08/11 职场文书
心得体会该怎么写呢?
2019/06/27 职场文书
八年级作文之感悟亲情
2019/11/20 职场文书
python 如何在list中找Topk的数值和索引
2021/05/20 Python
python 爬取哔哩哔哩up主信息和投稿视频
2021/06/07 Python
python unittest单元测试的步骤分析
2021/08/02 Python