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 相关文章推荐
wxPython框架类和面板类的使用实例
Sep 28 Python
对于Python编程中一些重用与缩减的建议
Apr 14 Python
python删除过期文件的方法
May 29 Python
使用pdb模块调试Python程序实例
Jun 02 Python
如何使用七牛Python SDK写一个同步脚本及使用教程
Aug 23 Python
python对DICOM图像的读取方法详解
Jul 17 Python
python爬虫headers设置后无效的解决方法
Oct 21 Python
python统计字母、空格、数字等字符个数的实例
Jun 29 Python
python+pyqt5实现KFC点餐收银系统
Jan 24 Python
python批量修改图片尺寸,并保存指定路径的实现方法
Jul 04 Python
python网络爬虫 Scrapy中selenium用法详解
Sep 28 Python
浅谈Python中的继承
Jun 19 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轻量级的性能分析工具xhprof的安装使用
2015/08/12 PHP
PHP获取当前URL路径的处理方法(适用于多条件筛选列表)
2017/02/10 PHP
CodeIgniter框架钩子机制实现方法【hooks类】
2018/08/21 PHP
Ajax执行顺序流程及回调问题分析
2012/12/10 Javascript
js 中的switch表达式使用示例
2020/06/03 Javascript
jquery序列化表单去除指定元素示例代码
2014/04/10 Javascript
jQuery无刷新切换主题皮肤实例讲解
2015/10/21 Javascript
利用iscroll4实现轮播图效果实例代码
2017/01/11 Javascript
Angular.JS中的指令引用template与指令当做属性详解
2017/03/30 Javascript
vue.js 使用v-if v-else发现没有执行解决办法
2017/05/15 Javascript
bootstrap选项卡扩展功能详解
2017/06/14 Javascript
php register_shutdown_function函数详解
2017/07/23 Javascript
JS沙箱模式实例分析
2017/09/04 Javascript
浅谈vue项目打包优化策略
2018/09/29 Javascript
微信小程序仿知乎实现评论留言功能
2018/11/28 Javascript
javascript实现计算指定范围内的质数示例
2018/12/29 Javascript
在vue中获取微信支付code及code被占用问题的解决方法
2019/04/16 Javascript
JS学习笔记之原型链和利用原型实现继承详解
2019/05/29 Javascript
jquery实现弹窗(系统提示框)效果
2019/12/10 jQuery
jQuery加PHP实现图片上传并提交的示例代码
2020/07/16 jQuery
python3实现暴力穷举博客园密码
2016/06/19 Python
Python 类,property属性(简化属性的操作),@property,property()用法示例
2019/10/12 Python
在python中利用try..except来代替if..else的用法
2019/12/19 Python
TensorBoard 计算图的可视化实现
2020/02/15 Python
Python绘制词云图之可视化神器pyecharts的方法
2021/02/23 Python
Coltorti Boutique官网:来自意大利的设计师品牌买手店
2018/11/09 全球购物
C#如何调用Word并打开一个Word文档
2013/05/08 面试题
高中自我评价范文
2014/01/27 职场文书
银行业务授权委托书
2014/10/10 职场文书
龙门石窟导游词
2015/02/02 职场文书
工作失职检讨书范文
2015/05/05 职场文书
2015年试用期工作总结范文
2015/05/28 职场文书
关爱留守儿童主题班会
2015/08/13 职场文书
2016大学生入党积极分子心得体会
2016/01/06 职场文书
vue使用Google Recaptcha验证的实现示例
2021/08/23 Vue.js
Mysql中一千万条数据怎么快速查询
2021/12/06 MySQL