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标准日志模块logging的使用方法
Nov 01 Python
python采集百度百科的方法
Jun 05 Python
PyTorch读取Cifar数据集并显示图片的实例讲解
Jul 27 Python
Python通过for循环理解迭代器和生成器实例详解
Feb 16 Python
在linux系统下安装python librtmp包的实现方法
Jul 22 Python
python求加权平均值的实例(附纯python写法)
Aug 22 Python
pytest中文文档之编写断言
Sep 12 Python
浅谈tensorflow中Dataset图片的批量读取及维度的操作详解
Jan 20 Python
python GUI库图形界面开发之PyQt5窗口类QMainWindow详细使用方法
Feb 26 Python
python 操作excel表格的方法
Dec 05 Python
python实现启动一个外部程序,并且不阻塞当前进程
Dec 05 Python
python 利用百度API识别图片文字(多线程版)
Dec 14 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应用技巧
2008/03/27 PHP
PHP取余函数介绍MOD(x,y)与x%y
2014/05/15 PHP
php生成二维码时出现中文乱码的解决方法
2014/12/18 PHP
thinkphp利用模型通用数据编辑添加和删除的实例代码
2016/11/20 PHP
javascript使用eval或者new Function进行语法检查
2010/10/16 Javascript
js对象关系图 方便dom操作
2012/03/18 Javascript
jQuery学习笔记(1)--用jQuery实现异步通信(用json传值)具体思路
2013/04/08 Javascript
JS实现随机化快速排序的实例代码
2013/08/01 Javascript
javascript的数组和常用函数详解
2014/05/09 Javascript
javascript中的return和闭包函数浅析
2014/06/06 Javascript
js实现统计字符串中特定字符出现个数的方法
2016/08/02 Javascript
JS中input表单隐藏域及其使用方法
2017/02/13 Javascript
Angular实现购物车计算示例代码
2017/02/21 Javascript
jQuery获取table下某一行某一列的值实现代码
2017/04/07 jQuery
JavaScript中in和hasOwnProperty区别详解
2017/08/04 Javascript
JavaScript实现封闭区域布尔运算的示例代码
2018/06/25 Javascript
Nuxt v-bind绑定img src不显示的解决
2019/12/05 Javascript
js如何验证密码强度
2020/03/18 Javascript
JS自定义右键菜单实现代码解析
2020/07/16 Javascript
[05:26]TI10典藏宝瓶套装外观展示
2020/07/03 DOTA
[02:17]快乐加倍!DOTA2食人魔魔法师至宝+迎霜节活动上线
2019/12/22 DOTA
python使用webbrowser浏览指定url的方法
2015/04/04 Python
python获取从命令行输入数字的方法
2015/04/29 Python
python3+PyQt5实现支持多线程的页面索引器应用程序
2018/04/20 Python
Python基于xlrd模块操作Excel的方法示例
2018/06/21 Python
flask框架实现连接sqlite3数据库的方法分析
2018/07/16 Python
不知道这5种下划线的含义,你就不算真的会Python!
2018/10/09 Python
对Django中static(静态)文件详解以及{% static %}标签的使用方法
2019/07/28 Python
Python进程间通信 multiProcessing Queue队列实现详解
2019/09/23 Python
python 字典套字典或列表的示例
2019/12/16 Python
荷兰最大的儿童服装店:The Kids Republic
2019/04/13 全球购物
离婚财产分隔协议书
2014/10/23 职场文书
2015年教师个人业务工作总结
2015/10/23 职场文书
redis哨兵常用命令和监控示例详解
2021/05/27 Redis
MyBatis自定义SQL拦截器示例详解
2021/10/24 Java/Android
Nginx跨域问题解析与解决
2022/08/05 Servers