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多层嵌套list的递归处理方法(推荐)
Jun 08 Python
python中Apriori算法实现讲解
Dec 10 Python
Python2.X/Python3.X中urllib库区别讲解
Dec 19 Python
Python网络爬虫中的同步与异步示例详解
Feb 03 Python
Python实现FTP弱口令扫描器的方法示例
Jan 31 Python
谈谈Python中的while循环语句
Mar 10 Python
python flask web服务实现更换默认端口和IP的方法
Jul 26 Python
Python有参函数使用代码实例
Jan 06 Python
python开发实例之Python的Twisted框架中Deferred对象的详细用法与实例
Mar 19 Python
Python tkinter界面实现历史天气查询的示例代码
Aug 23 Python
python利用paramiko实现交换机巡检的示例
Sep 22 Python
Python必备技巧之字符数据操作详解
Mar 23 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 flv视频时间获取函数
2010/06/29 PHP
用PHP的超级变量$_POST获取HTML表单(HTML Form) 数据
2011/05/07 PHP
php调用dll的实例操作动画与代码分享
2012/08/14 PHP
PHP链表操作简单示例
2016/10/15 PHP
PHP进程通信基础之信号
2017/02/19 PHP
window.location和document.location的区别分析
2008/12/23 Javascript
indexOf 和 lastIndexOf 使用示例介绍
2014/09/02 Javascript
jquery中使用循环下拉菜单示例代码
2014/09/24 Javascript
深入理解JavaScript系列(47):对象创建模式(上篇)
2015/03/04 Javascript
Javascript实现快速排序(Quicksort)的算法详解
2015/09/06 Javascript
jQuery旋转木马式幻灯片轮播特效
2015/12/04 Javascript
JS中call/apply、arguments、undefined/null方法详解
2016/02/15 Javascript
js+css实现红包雨效果
2018/07/12 Javascript
js实现坦克移动小游戏
2019/10/28 Javascript
JavaScript实现横版菜单栏
2020/03/17 Javascript
jQuery实现鼠标放置名字上显示详细内容气泡提示框效果的方法分析
2020/04/04 jQuery
JS字符串补全方法padStart()和padEnd()
2020/05/27 Javascript
python实现的文件夹清理程序分享
2014/11/22 Python
浅谈python中的占位符
2017/11/09 Python
Linux下Python安装完成后使用pip命令的详细教程
2018/11/22 Python
python实现狄克斯特拉算法
2019/01/17 Python
Python判断两个文件是否相同与两个文本进行相同项筛选的方法
2019/03/01 Python
Django 内置权限扩展案例详解
2019/03/04 Python
Python中的 sort 和 sorted的用法与区别
2019/08/10 Python
8段用于数据清洗Python代码(小结)
2019/10/31 Python
详解Python 最短匹配模式
2020/07/29 Python
施华洛世奇西班牙官网:SWAROVSKI西班牙
2019/06/06 全球购物
Chinti & Parker官网:奢华羊绒女装和创新针织设计
2021/01/01 全球购物
上海天奕面试题笔试题
2015/04/19 面试题
学前教育教师求职自荐信
2013/09/22 职场文书
2014年综合治理工作总结
2014/11/20 职场文书
好媳妇事迹材料
2014/12/24 职场文书
考研复习计划
2015/01/19 职场文书
幼儿园音乐教学反思
2016/02/18 职场文书
2019年新郎保证书3篇
2019/10/17 职场文书
Python OpenCV 彩色与灰度图像的转换实现
2021/06/05 Python