Python实现PS滤镜特效之扇形变换效果示例


Posted in Python onJanuary 26, 2018

本文实例讲述了Python实现PS滤镜特效之扇形变换效果。分享给大家供大家参考,具体如下:

这里用 Python 实现 PS 滤镜中的一种几何变换特效,称为扇形变换,将图像扭曲成一个扇形,具体的算法原理和效果图可以参考附录说明

import numpy as np
from skimage import img_as_float
import matplotlib.pyplot as plt
from skimage import io
import math
import numpy.matlib
file_name2='D:/Visual Effects/PS Algorithm/4.jpg'
img=io.imread(file_name2)
img = img_as_float(img)
# control the radius of the inner circle
radius = 150
# control the distance between the inner circle and outer circle
high = 200
angle = 0
spreadAngle = math.pi
# set the center of the circle, proportion of the image size
centerX = 0.5
centerY = 1.0
row, col, channel = img.shape
icenterX = col * centerX
icenterY = row * centerY
img_out = img * 0
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 - icenterX
yy_dif = y_mask - icenterY
theta = np.arctan2(-yy_dif, -xx_dif+0.0001)
r = np.sqrt(xx_dif*xx_dif + yy_dif * yy_dif)
theta = np.mod(theta, 2 * math.pi)
x1_mask = col * theta/(spreadAngle+0.00001)
y1_mask = row * (1-(r-radius)/(high+0.00001))
'''
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
'''
int_x = np.floor (x1_mask)
int_x = int_x.astype(int)
int_y = np.floor (y1_mask)
int_y = int_y.astype(int)
for ii in range(row):
  for jj in range (col):
    new_xx = int_x [ii, jj]
    new_yy = int_y [ii, jj]
    if x1_mask [ii, jj] < 0 or x1_mask [ii, jj] > col -1 :
      continue
    if y1_mask [ii, jj] < 0 or y1_mask [ii, jj] > row -1 :
      continue
    img_out[ii, jj, :] = img[new_yy, new_xx, :]
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()

附录:PS 滤镜— —扇形warp

clc;
  clear all;
  close all;
  addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm');
  I=imread('4.jpg');
  I=double(I);
  Image=I/255;
  [height, width, depth]=size(Image);
  % set the parameters
  radius = 150; % control the radius of the inner circle
  high = 200;  % control the distance between the inner circle and outer circle
  angle = 0;       
  spreadAngle=pi;  
  centerX = 0.5; % set the center of the circle, proportion of the image size
  centerY = 1.0;
  icenterX=width*centerX;
  icenterY=height*centerY;
  Image_new=Image*0;
  for i=1:height
    for j=1:width
      dx=j-icenterX;
      dy=i-icenterY;
      theta=atan2(-dy, -dx)+angle;
      r=sqrt(dy*dy+dx*dx);
      theta=mod(theta, 2*pi);
      x=width * theta/(spreadAngle+0.00001);
      y=height * (1-(r-radius)/(high+0.00001));
  % %     if (x<=1)   x=1; end
  % %     if (x>=width)  x=width-1; end;
  % %     if (y>=height) y=height-1; end;
  % %     if (y<1) y=1;   end;
  % %     
      if (x<=1)   continue; end
      if (x>=width)  continue; end;
      if (y>=height) 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运行效果:

原图

Python实现PS滤镜特效之扇形变换效果示例

效果图

Python实现PS滤镜特效之扇形变换效果示例

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

Python 相关文章推荐
python获取本机mac地址和ip地址的方法
Apr 29 Python
pandas groupby 分组取每组的前几行记录方法
Apr 20 Python
浅谈dataframe中更改列属性的方法
Jul 10 Python
django 发送邮件和缓存的实现代码
Jul 18 Python
python scipy求解非线性方程的方法(fsolve/root)
Nov 12 Python
Python两台电脑实现TCP通信的方法示例
May 06 Python
Python利用PyExecJS库执行JS函数的案例分析
Dec 18 Python
关于Python Tkinter Button控件command传参问题的解决方式
Mar 04 Python
Python改变对象的字符串显示的方法
Aug 01 Python
如何使用Python调整图像大小
Sep 26 Python
Python实现手势识别
Oct 21 Python
python调用百度AI接口实现人流量统计
Feb 03 Python
修复CentOS7升级Python到3.6版本后yum不能正确使用的解决方法
Jan 26 #Python
Python实现PS滤镜功能之波浪特效示例
Jan 26 #Python
Python使用pickle模块存储数据报错解决示例代码
Jan 26 #Python
python如何重载模块实例解析
Jan 25 #Python
Python进程间通信Queue实例解析
Jan 25 #Python
Python操作Redis之设置key的过期时间实例代码
Jan 25 #Python
python编程使用selenium模拟登陆淘宝实例代码
Jan 25 #Python
You might like
PHP - Html Transfer Code
2006/10/09 PHP
php中将数组转成字符串并保存到数据库中的函数代码
2013/09/29 PHP
php json_encode()函数返回json数据实例代码
2014/10/10 PHP
PHP封装的XML简单操作类完整实例
2017/11/13 PHP
Yii框架分页技术实例分析
2019/08/30 PHP
location.search在客户端获取Url参数的方法
2010/06/08 Javascript
javascript中有趣的反柯里化深入分析
2012/12/05 Javascript
jQuery使用数组编写图片无缝向左滚动
2012/12/11 Javascript
Jquery操作下拉框(DropDownList)实现取值赋值
2013/08/13 Javascript
5种处理js跨域问题方法汇总
2014/12/04 Javascript
简述AngularJS相关的一些编程思想
2015/06/23 Javascript
JavaScript数值千分位格式化的两种简单实现方法
2016/08/01 Javascript
JavaScript制作简易计算器(不用eval)
2017/02/05 Javascript
js实现类似iphone的网页滑屏解锁功能示例【附源码下载】
2019/06/10 Javascript
js编写简易的计算器
2020/07/29 Javascript
python之virtualenv的简单使用方法(必看篇)
2017/11/25 Python
Python断言assert的用法代码解析
2018/02/03 Python
Python用61行代码实现图片像素化的示例代码
2018/12/10 Python
Python3 实现减少可调用对象的参数个数
2019/12/20 Python
如何利用pygame实现简单的五子棋游戏
2019/12/29 Python
Pytorch之parameters的使用
2019/12/31 Python
对Tensorflow中tensorboard日志的生成与显示详解
2020/02/04 Python
在spyder IPython console中,运行代码加入参数的实例
2020/04/20 Python
Python爬虫实现百度翻译功能过程详解
2020/05/29 Python
Python 如何实现访问者模式
2020/07/28 Python
python如何调用php文件中的函数详解
2020/12/29 Python
Html5让容器充满屏幕高度或自适应剩余高度的布局实现
2020/05/14 HTML / CSS
天美时手表加拿大官网:Timex加拿大
2016/09/01 全球购物
关于期中考试的反思
2014/02/02 职场文书
幼儿园优秀教师事迹
2014/02/13 职场文书
心理学专业大学生职业生涯规划范文
2014/02/19 职场文书
开业庆典主持词
2014/03/21 职场文书
项目负责人任命书
2014/06/04 职场文书
2014年学生会个人工作总结
2014/11/07 职场文书
2015年材料员工作总结
2015/04/30 职场文书
Windows Server 2019 域控制器安装图文教程
2022/04/28 Servers