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 自动安装 Rising 杀毒软件
Apr 24 Python
python创建线程示例
May 06 Python
详解Python中列表和元祖的使用方法
Apr 25 Python
python实现列表中由数值查到索引的方法
Jun 27 Python
Python实现随机创建电话号码的方法示例
Dec 07 Python
梅尔倒谱系数(MFCC)实现
Jun 19 Python
python内存动态分配过程详解
Jul 15 Python
Python3.7安装PyQt5 运行配置Pycharm的详细教程
Oct 15 Python
Python生成pdf目录书签的实例方法
Oct 29 Python
Django 权限管理(permissions)与用户组(group)详解
Nov 30 Python
浅析Python打包时包含静态文件处理方法
Jan 15 Python
用Python制作灯光秀短视频的思路详解
Apr 13 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中Socket创建与监听实现方法
2015/01/05 PHP
Zend Framework教程之Zend_Config_Xml用法分析
2016/03/23 PHP
浅谈php(codeigniter)安全性注意事项
2017/04/06 PHP
PHP实现上传多图即时显示与即时删除的方法
2017/05/09 PHP
浅谈PHP各环境下的伪静态配置
2019/03/13 PHP
TP5框架实现自定义分页样式的方法示例
2020/04/05 PHP
收集的网上用的ajax之chat.js文件
2007/04/08 Javascript
window.dialogArguments 使用说明
2011/04/11 Javascript
IE6下opacity与JQuery的奇妙结合
2013/03/01 Javascript
jQuery extend 的简单实例
2013/09/18 Javascript
关于js内存泄露的一个好例子
2013/12/09 Javascript
jQuery中ready事件用法实例
2015/01/19 Javascript
利用python分析access日志的方法
2016/10/26 Javascript
angular实现商品筛选功能
2017/02/01 Javascript
node.js入门教程之querystring模块的使用方法
2017/02/27 Javascript
详谈jQuery.load()和Jsp的include的区别
2017/04/12 jQuery
Django+Vue.js搭建前后端分离项目的示例
2017/08/07 Javascript
javascript数组拍平方法总结
2018/01/20 Javascript
js+SVG实现动态时钟效果
2018/07/14 Javascript
JavaScript canvas绘制渐变颜色的矩形
2020/02/18 Javascript
关于JavaScript中异步/等待的用法与理解
2020/11/18 Javascript
[01:11:15]VGJ.S vs Secret 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
Python实现爬取逐浪小说的方法
2015/07/07 Python
python实现二叉查找树实例代码
2018/02/08 Python
Caffe均值文件mean.binaryproto转mean.npy的方法
2018/07/09 Python
django 微信网页授权认证api的步骤详解
2019/07/30 Python
浅谈pandas.cut与pandas.qcut的使用方法及区别
2020/03/03 Python
一款简洁的纯css3代码实现的动画导航
2014/10/31 HTML / CSS
使用CSS3创建动态菜单效果
2015/07/10 HTML / CSS
工程造价与管理专业应届生求职信
2013/11/23 职场文书
职业生涯规划书怎么写?
2014/09/14 职场文书
教师个人年度总结
2015/02/11 职场文书
先进个人自荐书
2015/03/06 职场文书
师范生教育见习总结
2015/06/23 职场文书
送给火锅店的创意营销方案!
2019/07/08 职场文书
使用refresh_token实现无感刷新页面
2022/04/26 Javascript