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读取文件常用方法实例分析
May 22 Python
django通过ajax发起请求返回JSON格式数据的方法
Jun 04 Python
Python实现比较两个列表(list)范围
Jun 12 Python
python使用MySQLdb访问mysql数据库的方法
Aug 03 Python
Python多进程fork()函数详解
Feb 22 Python
python打造爬虫代理池过程解析
Aug 15 Python
python3.6、opencv安装环境搭建过程(图文教程)
Nov 05 Python
python 实现单通道转3通道
Dec 03 Python
python二分法查找算法实现方法【递归与非递归】
Dec 06 Python
python中count函数简单的实例讲解
Feb 06 Python
利用Python pandas对Excel进行合并的方法示例
Nov 04 Python
python 获取字典键值对的实现
Nov 12 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
收音机的保养
2021/03/01 无线电
flash用php连接数据库的代码
2011/04/21 PHP
php上传图片存入数据库示例分享
2014/03/11 PHP
PHP中模拟处理HTTP PUT请求的例子
2014/07/22 PHP
PHP图像处理之imagecreate、imagedestroy函数介绍
2014/11/19 PHP
thinkphp3.x连接mysql数据库的方法(具体操作步骤)
2016/05/19 PHP
Zend Framework前端控制器用法示例
2016/12/11 PHP
php实现图片按比例截取的方法
2017/02/06 PHP
Web层改进II-用xmlhttp 无声息提交复杂表单
2007/01/22 Javascript
jquery之empty()与remove()区别说明
2010/09/10 Javascript
jquery图片上下tab切换效果
2011/03/18 Javascript
浅析jQuery(function(){})与(function(){})(jQuery)之间的区别
2014/01/09 Javascript
如何将php数组或者对象传递给javascript
2014/03/20 Javascript
基于jQuery的Web上传插件Uploadify使用示例
2016/05/19 Javascript
JavaScript使用键盘输入控制实现数字验证功能
2016/08/19 Javascript
微信小程序 富文本转文本实例详解
2016/10/24 Javascript
JS类的定义与使用方法深入探索
2016/11/26 Javascript
Angular下H5上传图片的方法(可多张上传)
2017/01/09 Javascript
nodejs6下使用koa2框架实例
2017/05/18 NodeJs
vue.js框架实现表单排序和分页效果
2017/08/09 Javascript
Angular模版驱动表单的使用总结
2018/05/05 Javascript
weui上传多图片,压缩,base64编码的示例代码
2020/06/22 Javascript
JavaScript React如何修改默认端口号方法详解
2020/07/28 Javascript
JS轮播图的实现方法
2020/08/24 Javascript
JS实现纸牌发牌动画
2021/01/19 Javascript
Python  连接字符串(join %)
2008/09/06 Python
在Python中利用Into包整洁地进行数据迁移的教程
2015/03/30 Python
python3+PyQt5 数据库编程--增删改实例
2019/06/17 Python
Python urlencode和unquote函数使用实例解析
2020/03/31 Python
浅谈keras 的抽象后端(from keras import backend as K)
2020/06/16 Python
CSS3+font字体文件实现圆形半透明菜单具体步骤(图解)
2013/06/03 HTML / CSS
Html5实现单张、多张图片上传功能
2019/04/28 HTML / CSS
利用纯html5绘制出来的一款非常漂亮的时钟
2015/01/04 HTML / CSS
美国伴娘礼服商店:Evening Collective
2019/10/07 全球购物
日本动漫周边服饰销售网站:Atsuko
2019/12/16 全球购物
门诊手术室工作制度
2014/01/30 职场文书