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 01 Python
python通过urllib2爬网页上种子下载示例
Feb 24 Python
python中pass语句用法实例分析
Apr 30 Python
python cx_Oracle的基础使用方法(连接和增删改查)
Nov 19 Python
Python科学计算包numpy用法实例详解
Feb 08 Python
pandas表连接 索引上的合并方法
Jun 08 Python
python使用xlsxwriter实现有向无环图到Excel的转换
Dec 12 Python
浅谈pyqt5中信号与槽的认识
Feb 17 Python
Appium+python自动化之连接模拟器并启动淘宝APP(超详解)
Jun 17 Python
python读取并写入mat文件的方法
Jul 12 Python
Python输出指定字符串的方法
Feb 06 Python
Django REST framework 限流功能的使用
Jun 24 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
Laravel 5框架学习之Blade 简介
2015/04/08 PHP
PHP利用Socket获取网站的SSL证书与公钥
2017/06/18 PHP
PHP对称加密算法(DES/AES)类的实现代码
2017/11/14 PHP
PHP pthreads v3下worker和pool的使用方法示例
2020/02/21 PHP
[原创]IE view-source 无法查看看源码 JavaScript看网页源码
2009/07/19 Javascript
使用Jquery打造最佳用户体验的登录页面的实现代码
2011/07/08 Javascript
js 完美图片新闻轮转效果,腾讯大粤网首页图片轮转改造而来
2011/11/21 Javascript
js 连接数据库如何操作数据库中的数据
2012/11/23 Javascript
javaScript 动态访问JSon元素示例代码
2013/08/30 Javascript
nodejs之请求路由概述
2014/07/05 NodeJs
JS实现为表格动态添加标题的方法
2015/03/31 Javascript
JavaScript中用getDate()方法返回指定日期的教程
2015/06/09 Javascript
完美解决jQuery 鼠标快速滑过后,会执行多次滑出的问题
2016/12/08 Javascript
在JS中如何把毫秒转换成规定的日期时间格式实例
2017/05/11 Javascript
vue轮播图插件vue-awesome-swiper的使用代码实例
2017/07/10 Javascript
vue3.0 CLI - 2.2 - 组件 home.vue 的初步改造
2018/09/14 Javascript
vue登录页面cookie的使用及页面跳转代码
2019/07/10 Javascript
微信小程序 弹窗输入组件的实现解析
2019/08/12 Javascript
nodejs开发一个最简单的web服务器实例讲解
2020/01/02 NodeJs
Python Django使用forms来实现评论功能
2016/08/17 Python
python添加模块搜索路径方法
2017/09/11 Python
基于Python中求和函数sum的用法详解
2018/06/28 Python
Python基于Tensor FLow的图像处理操作详解
2020/01/15 Python
基于python实现查询ip地址来源
2020/06/02 Python
使用AJAX和Django获取数据的方法实例
2020/10/25 Python
Html5 语法与规则简要概述
2014/07/29 HTML / CSS
华润集团网上药店:健一网
2016/09/19 全球购物
手机配件第一品牌:ZAGG
2017/05/28 全球购物
大都会艺术博物馆商店:The Met Store
2018/06/22 全球购物
以工厂直接定价的传奇性能:Ben Hogan Golf
2019/01/04 全球购物
Vertbaudet西班牙网上商店:婴儿服装、童装、母婴用品和儿童家具
2019/10/16 全球购物
澳大利亚领先的内衣店:Bendon Lingerie澳大利亚
2020/05/15 全球购物
js实现弹框效果
2021/03/24 Javascript
三个儿子教学反思
2014/02/03 职场文书
慰问信格式
2015/02/14 职场文书
Windows Server 2012 R2服务器安装与配置的完整步骤
2022/07/15 Servers