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单元测试框架unittest使用方法讲解
Apr 13 Python
python使用in操作符时元组和数组的区别分析
May 19 Python
Python常见排序操作示例【字典、列表、指定元素等】
Aug 15 Python
Python实现DDos攻击实例详解
Feb 02 Python
python模拟斗地主发牌
Apr 22 Python
在Pytorch中使用Mask R-CNN进行实例分割操作
Jun 24 Python
Java byte数组操纵方式代码实例解析
Jul 22 Python
python实现AdaBoost算法的示例
Oct 03 Python
Python json解析库jsonpath原理及使用示例
Nov 25 Python
Python 中的 copy()和deepcopy()
Nov 07 Python
Python下载商品数据并连接数据库且保存数据
Mar 31 Python
Python可变与不可变数据和深拷贝与浅拷贝
Apr 06 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
博士208HAF收音机实习报告
2021/03/02 无线电
真正面向对象编程:PHP5.01发布
2006/10/09 PHP
PHP自动生成月历代码
2006/10/09 PHP
3种平台下安装php4经验点滴
2006/10/09 PHP
使用VisualStudio开发php的图文设置方法
2010/08/21 PHP
深入理解PHP原理之异常机制
2010/08/21 PHP
PHP 事件机制(2)
2011/03/23 PHP
有关PHP性能优化的介绍
2013/06/20 PHP
PHP Imagick完美实现图片裁切、生成缩略图、添加水印
2016/02/22 PHP
CI框架(ajax分页,全选,反选,不选,批量删除)完整代码详解
2016/11/01 PHP
JavaScript 创建对象和构造类实现代码
2009/07/30 Javascript
js隐藏与显示回到顶部按钮及window.onscroll事件应用
2013/01/25 Javascript
js控制web打印(局部打印)方法整理
2013/05/29 Javascript
jquery实现点击消失的代码
2014/03/03 Javascript
jQuery实现菜单式图片滑动切换
2015/03/14 Javascript
Jquery跨浏览器文本复制插件Zero Clipboard的使用方法
2016/02/28 Javascript
学习使用bootstrap3栅格系统
2016/04/12 Javascript
javascript类型系统_正则表达式RegExp类型详解
2016/06/24 Javascript
jQuery自适应轮播图插件Swiper用法示例
2016/08/24 Javascript
微信小程序 Nginx环境配置详细介绍
2017/02/14 Javascript
canvas绘制环形进度条
2017/02/23 Javascript
jQuery模拟窗口抖动效果
2017/03/15 Javascript
如何开发出更好的JavaScript模块
2017/12/22 Javascript
webpack4之SplitChunksPlugin使用指南
2018/06/12 Javascript
解决Angular2 router.navigate刷新页面的问题
2018/08/31 Javascript
Windows系统配置python脚本开机启动的3种方法分享
2015/03/10 Python
python生成IP段的方法
2015/07/07 Python
Python函数参数匹配模型通用规则keyword-only参数详解
2019/06/10 Python
Python爬虫HTPP请求方法有哪些
2020/06/03 Python
python的数学算法函数及公式用法
2020/11/18 Python
html5自带表单验证体验优化及提示气泡修改功能
2017/09/12 HTML / CSS
kfc实习自我鉴定
2013/12/14 职场文书
应届毕业生求职信范例分享
2013/12/17 职场文书
四风问题查摆剖析材料
2014/10/11 职场文书
Python机器学习算法之决策树算法的实现与优缺点
2021/05/13 Python
前端框架ECharts dataset对数据可视化的高级管理
2022/12/24 Javascript