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 相关文章推荐
Python3连接MySQL(pymysql)模拟转账实现代码
May 24 Python
python 自动化将markdown文件转成html文件的方法
Sep 23 Python
DataFrame中的object转换成float的方法
Apr 10 Python
解决Pandas的DataFrame输出截断和省略的问题
Feb 08 Python
你还在@微信官方?聊聊Python生成你想要的微信头像
Sep 25 Python
浅谈对pytroch中torch.autograd.backward的思考
Dec 27 Python
利用python画出AUC曲线的实例
Feb 28 Python
将keras的h5模型转换为tensorflow的pb模型操作
May 25 Python
django rest framework 过滤时间操作
Jul 12 Python
Pyecharts 中Geo函数常用参数的用法说明
Feb 01 Python
基于python的matplotlib制作双Y轴图
Apr 20 Python
使用Pytorch训练two-head网络的操作
May 28 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 日常开发小技巧
2009/09/23 PHP
POSIX 风格和兼容 Perl 风格两种正则表达式主要函数的类比(preg_match, preg_replace, ereg, ereg_replace)
2010/10/12 PHP
使用JSON实现数据的跨域传输的php代码
2011/12/20 PHP
php指定函数参数默认值示例代码
2013/12/04 PHP
ThinkPHP连接数据库及主从数据库的设置教程
2014/08/22 PHP
浅析Yii2 GridView实现下拉搜索教程
2016/04/22 PHP
jQuery 使用个人心得
2009/02/26 Javascript
重构Javascript代码示例(重构前后对比)
2013/01/23 Javascript
JS动态添加option和删除option(附实例代码)
2013/04/01 Javascript
JS在TextArea光标位置插入文字并实现移动光标到文字末尾
2013/06/21 Javascript
javascript生成随机数的方法
2014/05/16 Javascript
javascript制作坦克大战全纪录(1)
2014/11/27 Javascript
JavaScript实现获得所有兄弟节点的方法
2015/07/23 Javascript
实例代码详解jquery.slides.js
2015/11/16 Javascript
详解JavaScript 中getElementsByName在IE中的注意事项
2017/02/21 Javascript
vuex的简单使用教程
2018/02/02 Javascript
Vue脚手架的简单使用实例
2018/07/10 Javascript
微信小程序内拖动图片实现移动、放大、旋转的方法
2018/09/04 Javascript
webpack 3.X学习之多页面打包的方法
2018/09/04 Javascript
js cavans实现静态滚动弹幕
2020/05/21 Javascript
部署vue+Springboot前后端分离项目的步骤实现
2020/05/31 Javascript
python 文件转成16进制数组的实例
2018/07/09 Python
django 做 migrate 时 表已存在的处理方法
2019/08/31 Python
pyecharts在数据可视化中的应用详解
2020/06/08 Python
10行Python代码实现Web自动化管控的示例代码
2020/08/14 Python
详解Python openpyxl库的基本应用
2021/02/26 Python
联想马亚西亚官方网站:Lenovo Malaysia
2018/09/19 全球购物
德国Discount-Apotheke中文官网:DC德式康线上药房
2020/02/18 全球购物
学生档案自我鉴定
2013/10/07 职场文书
合作投资意向书
2014/04/01 职场文书
关于保护环境的建议书
2014/08/26 职场文书
2014年材料员工作总结
2014/11/19 职场文书
河童之夏观后感
2015/06/11 职场文书
2016秋季运动会开幕词
2016/03/04 职场文书
投资入股协议书
2016/03/22 职场文书
浅谈Redis存储数据类型及存取值方法
2021/05/08 Redis