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性能优化技巧
Mar 09 Python
Python实现TCP协议下的端口映射功能的脚本程序示例
Jun 14 Python
python3之微信文章爬虫实例讲解
Jul 12 Python
浅谈Python实现贪心算法与活动安排问题
Dec 19 Python
Python使用pickle模块储存对象操作示例
Aug 15 Python
使用浏览器访问python写的服务器程序
Oct 10 Python
pytorch 利用lstm做mnist手写数字识别分类的实例
Jan 10 Python
Python破解BiliBili滑块验证码的思路详解(完美避开人机识别)
Feb 17 Python
PyQt5实现登录页面
May 30 Python
如何解决pycharm调试报错的问题
Aug 06 Python
pytorch学习教程之自定义数据集
Nov 10 Python
Python使用mitmproxy工具监控手机 下载手机小视频
Apr 18 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
处理单名多值表单的详解
2013/06/08 PHP
thinkphp获取栏目和文章当前位置的方法
2014/10/29 PHP
PHP处理数组和XML之间的互相转换
2016/06/02 PHP
JQuery 动画卷页 返回顶部 动画特效(兼容Chrome)
2010/02/15 Javascript
Jquery + Ajax调用webService实例代码(asp.net)
2010/08/27 Javascript
jQuery实现页面滚动时动态加载内容的方法
2015/03/20 Javascript
理解Javascript的动态语言特性
2015/06/17 Javascript
Javascript实现的Map集合工具类完整实例
2015/07/31 Javascript
使用AngularJS 跨站请求如何解决jsonp请求问题
2017/01/16 Javascript
js实现年月日表单三级联动
2020/04/17 Javascript
JavaScript实现开关等效果
2017/09/08 Javascript
Vue+SpringBoot开发V部落博客管理平台
2017/12/27 Javascript
jQuery实现DIV响应鼠标滑过由下向上展开效果示例【测试可用】
2018/04/26 jQuery
详解使用create-react-app添加css modules、sasss和antd
2018/07/31 Javascript
this在vue和小程序中的使用详解
2019/01/28 Javascript
vue自定义指令实现方法详解
2019/02/11 Javascript
微信小程序自定义头部导航栏和导航栏背景图片 navigationStyle问题
2019/07/26 Javascript
JS实现纵向轮播图(初级版)
2020/01/18 Javascript
微信小程序换肤功能实现代码(思路详解)
2020/08/25 Javascript
[01:25]2014DOTA2国际邀请赛 zhou分析LGD比赛情况
2014/07/14 DOTA
Python使用Tkinter实现机器人走迷宫
2018/01/22 Python
浅谈Python中重载isinstance继承关系的问题
2018/05/04 Python
Python实现SQL注入检测插件实例代码
2019/02/02 Python
使用Python3+PyQT5+Pyserial 实现简单的串口工具方法
2019/02/13 Python
django 实现将本地图片存入数据库,并能显示在web上的示例
2019/08/07 Python
基于tensorflow指定GPU运行及GPU资源分配的几种方式小结
2020/02/03 Python
Python如何测试stdout输出
2020/08/10 Python
Currentbody西班牙:美容仪专家
2019/09/28 全球购物
迪奥美国官网:Dior美国
2019/12/07 全球购物
英国时尚和家居用品零售商:Matalan
2021/02/28 全球购物
公司门卫岗位职责范本
2014/07/08 职场文书
民主评议党员总结
2014/10/20 职场文书
安全检查汇报材料
2014/12/26 职场文书
导游词之舟山普陀山
2019/11/06 职场文书
看看如何用Python绘制小米新版天价logo
2021/04/20 Python
正确使用MySQL INSERT INTO语句
2021/05/26 MySQL