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地址的方法
Mar 16 Python
python模拟enum枚举类型的方法小结
Apr 30 Python
Python实现在线暴力破解邮箱账号密码功能示例【测试可用】
Sep 06 Python
Python绘制3d螺旋曲线图实例代码
Dec 20 Python
Python实现的IP端口扫描工具类示例
Feb 15 Python
Python 使用threading+Queue实现线程池示例
Dec 21 Python
如何使用pandas读取txt文件中指定的列(有无标题)
Mar 05 Python
基于python 凸包问题的解决
Apr 16 Python
Python学习之os模块及用法
Jun 03 Python
基于Python 的语音重采样函数解析
Jul 06 Python
python如何构建mock接口服务
Jan 28 Python
Python机器学习之PCA降维算法详解
May 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数组函数序列 之array_count_values() 统计数组中所有值出现的次数函数
2011/10/29 PHP
PHP警告Cannot use a scalar value as an array的解决方法
2012/01/11 PHP
PHP解密Unicode及Escape加密字符串
2015/05/17 PHP
在 IE 中调用 javascript 打开 Excel 表
2006/12/21 Javascript
js 模拟气泡屏保效果代码
2010/07/10 Javascript
javascript实现10个球随机运动、碰撞实例详解
2015/07/08 Javascript
jQuery实现简单的DIV拖动效果
2016/02/19 Javascript
探讨:JavaScript ECAMScript5 新特性之get/set访问器
2016/05/05 Javascript
jQuery 调用WebService 实例讲解
2016/06/28 Javascript
Javascript中的对象和原型(二)
2016/08/12 Javascript
纯JS打造网页中checkbox和radio的美化效果
2016/10/13 Javascript
解决ajax不能访问本地文件问题(利用js跨域原理)
2017/01/24 Javascript
Node.js利用debug模块打印出调试日志的方法
2017/04/25 Javascript
微信小程序 实现动态显示和隐藏某个控件
2017/04/27 Javascript
Javascript循环删除数组中元素的几种方法示例
2017/05/18 Javascript
mui back 返回刷新页面的实例
2017/12/06 Javascript
谈谈vue中mixin的一点理解
2017/12/12 Javascript
nodejs实现超简单生成二维码的方法
2018/03/17 NodeJs
详解json串反转义(消除反斜杠)
2019/08/12 Javascript
小程序两种滚动公告栏的实现方法
2019/09/17 Javascript
Vue的生命周期操作示例
2019/09/17 Javascript
微信小程序实现首页弹出广告
2020/12/03 Javascript
python实现对指定输入的字符串逆序输出的6种方法
2018/04/26 Python
python3学生名片管理v2.0版
2018/11/29 Python
Django-simple-captcha验证码包使用方法详解
2020/11/28 Python
CSS3中border-radius属性设定圆角的使用技巧
2016/05/10 HTML / CSS
css3.0 图形构成实例练习二
2013/03/19 HTML / CSS
详解如何在css中引入自定义字体(font-face)
2018/05/17 HTML / CSS
利用html5 canvas破解简单验证码及getImageData接口应用
2013/01/25 HTML / CSS
浅谈html5与APP混合开发遇到的问题总结
2018/03/20 HTML / CSS
《红军不怕远征难》教学反思
2014/04/14 职场文书
药品营销策划方案
2014/06/15 职场文书
2014院党委领导班子对照检查材料思想汇报
2014/09/24 职场文书
党校个人总结
2015/03/04 职场文书
现役军人家属慰问信
2015/03/24 职场文书
Java对文件的读写操作方法
2022/04/29 Java/Android