Python实现PS滤镜的万花筒效果示例


Posted in Python onJanuary 23, 2018

本文实例讲述了Python实现PS滤镜的万花筒效果。分享给大家供大家参考,具体如下:

这里用 Python 实现 PS 的一种滤镜效果,称为万花筒。也是对图像做各种扭曲变换,最后图像呈现的效果就像从万花筒中看到的一样:

图像的效果可以参考附录说明。具体Python代码如下:

import matplotlib.pyplot as plt
from skimage import io
from skimage import img_as_float
import numpy as np
import numpy.matlib
import math
file_name='D:/Visual Effects/PS Algorithm/4.jpg';
img=io.imread(file_name)
img = img_as_float(img)
row, col, channel = img.shape
# set the parameters
radius = 100.0
angle = math.pi/3
angle2 = math.pi/4
sides = 10.0
# set the center of the circle, proportion of the image size
centerX = 0.5
centerY = 0.5
iWidth=col
iHeight=row
center_x=iWidth*centerX
center_y=iHeight*centerY
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)
xx_dif = x_mask - center_x
yy_dif = y_mask - center_y
r = np.sqrt(xx_dif * xx_dif + yy_dif * yy_dif)
theta = np.arctan2(yy_dif, xx_dif+0.0001) - angle - angle2
temp_theta=theta/math.pi*sides*0.5
temp_r = np.mod(temp_theta, 1.0)
mask_1 = temp_r < 0.5
theta = temp_r * 2 * mask_1 + (1-temp_r) * 2 * (1 - mask_1)
radius_c=radius/np.cos(theta)
temp_r = np.mod (r/radius_c, 1.0)
mask_1 = temp_r < 0.5
r = radius_c * (temp_r * 2 * mask_1 + (1-temp_r) * 2 * (1 - mask_1))
theta = theta + angle
x1_mask = r * np.cos(theta) + center_x
y1_mask = r * np.sin(theta) + center_y
mask = x1_mask < 0
x1_mask = x1_mask * (1 - mask)
mask = x1_mask > (col - 1)
x1_mask = x1_mask * (1 - mask) + (x1_mask * 0 + col -2) * mask
mask = y1_mask < 0
y1_mask = y1_mask * (1 - mask)
mask = y1_mask > (row -1)
y1_mask = y1_mask * (1 - mask) + (y1_mask * 0 + row -2) * mask
img_out = img * 1.0
int_x = np.floor (x1_mask)
int_x = int_x.astype(int)
int_y = np.floor (y1_mask)
int_y = int_y.astype(int)
p_mask = x1_mask - int_x
q_mask = y1_mask - int_y
img_out = img * 1.0
for ii in range(row):
  for jj in range (col):
    new_xx = int_x [ii, jj]
    new_yy = int_y [ii, jj]
#    p = p_mask[ii, jj]
#    q = q_mask[ii, jj]
    img_out[ii, jj, :] = img[new_yy, new_xx, :]
plt.figure (1)
plt.imshow (img)
plt.axis('off')
plt.figure (2)
plt.imshow (img_out)
plt.axis('off')
plt.show()

附:PS 滤镜万花筒效果原理

clc;
  clear all;
  close all;
  addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm');
  I=imread('4.jpg');
  I=double(I);
  Image=I/255;
  sz=size(Image);
  % set the parameters
  radius = 150;
  angle = pi/4;
  angle2=pi/4;
  sides=10;
  centerX = 0.5;  % set the center of the circle, proportion of the image size
  centerY = 0.5;
  iWidth=sz(2);
  iHeight=sz(1);
  icenterX=iWidth*centerX;
  icenterY=iHeight*centerY;
  Image_new=Image;
  for i=1:sz(1)
    for j=1:sz(2)
      dx=j-icenterX;
      dy=i-icenterY;
      r=sqrt(dy*dy+dx*dx);
      theta=atan2(dy, dx)-angle-angle2;
      temp_theta=theta/pi*sides*0.5 ;
      theta=triangle(temp_theta);
      if (radius)
        c=cos(theta);
        radius_c=radius/c;
        r=radius_c * triangle(r/radius_c);
      end
      theta=theta+angle;
      x=r * cos(theta)+icenterX;
      y=r * sin(theta)+icenterY;
      if (x<=1)   x=1; end
      if (x>=sz(2)) x=sz(2)-1; end;
      if (y>=sz(1)) y=sz(1)-1; end;
      if (y<1) y=1;   end;
  % % %     if (x<=1)   continue; end
  % % %     if (x>=sz(2))  continue; end;
  % % %     if (y>=sz(1)) continue; end;
  % % %     if (y<1) continue;   end;
      x1=floor(x);
      y1=floor(y);
      p=x-x1;
      q=y-y1;
      Image_new(i,j,:)=(1-p)*(1-q)*Image(y1,x1,:)+p*(1-q)*Image(y1,x1+1,:)...
        +q*(1-p)*Image(y1+1,x1,:)+p*q*Image(y1+1,x1+1,:);
    end
  end
  imshow(Image_new)
  imwrite(Image_new, 'out.jpg');

参考来源:http://www.jhlabs.com/index.html

原图:

Python实现PS滤镜的万花筒效果示例

效果图:

Python实现PS滤镜的万花筒效果示例

Python实现PS滤镜的万花筒效果示例

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python中处理字符串之isalpha()方法的使用
May 18 Python
在Lighttpd服务器中运行Django应用的方法
Jul 22 Python
简单谈谈python中的语句和语法
Aug 10 Python
Pycharm配置远程调试的方法步骤
Dec 17 Python
对Python定时任务的启动和停止方法详解
Feb 19 Python
python画图把时间作为横坐标的方法
Jul 07 Python
Python 200行代码实现一个滑动验证码过程详解
Jul 11 Python
Python 用turtle实现用正方形画圆的例子
Nov 21 Python
OpenCV 表盘指针自动读数的示例代码
Apr 10 Python
python nohup 实现远程运行不宕机操作
Apr 16 Python
Python爬取科目四考试题库的方法实现
Mar 30 Python
Python实战之实现康威生命游戏
Apr 26 Python
python处理csv数据动态显示曲线实例代码
Jan 23 #Python
Python+matplotlib实现华丽的文本框演示代码
Jan 22 #Python
CentOS7.3编译安装Python3.6.2的方法
Jan 22 #Python
Python OpenCV实现图片上输出中文
Jan 22 #Python
python批量替换页眉页脚实例代码
Jan 22 #Python
python解析html提取数据,并生成word文档实例解析
Jan 22 #Python
Python复制Word内容并使用格式设字体与大小实例代码
Jan 22 #Python
You might like
配置PHP使之能同时支持GIF和JPEG
2006/10/09 PHP
解析wamp5下虚拟机配置文档
2013/06/27 PHP
php实现12306火车票余票查询和价格查询(12306火车票查询)
2014/01/14 PHP
PHP中使用asort进行中文排序失效的问题处理
2014/08/18 PHP
PHP获取表单所有复选框的值的方法
2014/08/28 PHP
php给图片添加文字水印方法汇总
2015/08/27 PHP
php + WebUploader实现图片批量上传功能
2019/05/06 PHP
js 学习笔记(三)
2009/12/29 Javascript
JS 各种网页尺寸判断实例方法
2013/04/18 Javascript
jQuery中阻止冒泡事件的方法介绍
2014/04/12 Javascript
一个简单的实现下拉框多选的插件可移植性比较好
2014/05/05 Javascript
javascript瀑布流布局实现方法详解
2016/02/17 Javascript
JS平滑无缝滚动效果的实现代码
2016/05/06 Javascript
Js类的静态方法与实例方法区分及jQuery拓展的两种方法
2016/06/03 Javascript
Ajax分页插件Pagination从前台jQuery到后端java总结
2016/07/22 Javascript
Zepto实现密码的隐藏/显示
2017/04/07 Javascript
详解angularJS+Ionic移动端图片上传的解决办法
2017/09/13 Javascript
原生JS实现的自动轮播图功能详解
2018/12/28 Javascript
深入探讨JavaScript的最基本部分之执行上下文
2019/02/12 Javascript
JS实现图片懒加载(lazyload)过程详解
2020/04/02 Javascript
利用Python爬虫给孩子起个好名字
2017/02/14 Python
Python获取当前页面内所有链接的四种方法对比分析
2017/08/19 Python
Python批量合并有合并单元格的Excel文件详解
2018/04/05 Python
python2.x实现人民币转大写人民币
2018/06/20 Python
用Django写天气预报查询网站
2018/10/21 Python
简单了解python协程的相关知识
2019/08/31 Python
Mac PyCharm中的.gitignore 安装设置教程
2020/04/16 Python
PyQt5通过信号实现MVC的示例
2021/02/06 Python
澳大利亚婴儿喂养品牌:Cherub Baby
2018/11/01 全球购物
365 Tickets英国:全球景点门票
2019/07/06 全球购物
Web Service面试题:如何搭建Axis2的开发环境
2012/06/20 面试题
超市中秋节促销方案
2014/03/21 职场文书
产品开发计划书
2014/04/27 职场文书
环保倡议书范文
2014/05/12 职场文书
1000字打架检讨书
2014/11/03 职场文书
2019年家电促销广告语集锦
2019/10/21 职场文书