Python实现PS滤镜的万花筒效果示例
作者:Matrix_11 发布时间:2023-11-15 10:17:25
标签:Python,PS滤镜,万花筒
本文实例讲述了Python实现PS滤镜的万花筒效果。分享给大家供大家参考,具体如下:
这里用 Python 实现 PS 的一种滤镜效果,称为万花筒。也是对图像做各种扭曲变换,最后图像呈现的效果就像从万花筒中看到的一样:
图像的效果可以参考附录说明。具体Python代码如下:
import matplotlib.pyplot as plt
from skimage import io
from skimage import img_as_float
import numpy as np
import numpy.matlib
import math
file_name='D:/Visual Effects/PS Algorithm/4.jpg';
img=io.imread(file_name)
img = img_as_float(img)
row, col, channel = img.shape
# set the parameters
radius = 100.0
angle = math.pi/3
angle2 = math.pi/4
sides = 10.0
# set the center of the circle, proportion of the image size
centerX = 0.5
centerY = 0.5
iWidth=col
iHeight=row
center_x=iWidth*centerX
center_y=iHeight*centerY
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 - center_x
yy_dif = y_mask - center_y
r = np.sqrt(xx_dif * xx_dif + yy_dif * yy_dif)
theta = np.arctan2(yy_dif, xx_dif+0.0001) - angle - angle2
temp_theta=theta/math.pi*sides*0.5
temp_r = np.mod(temp_theta, 1.0)
mask_1 = temp_r < 0.5
theta = temp_r * 2 * mask_1 + (1-temp_r) * 2 * (1 - mask_1)
radius_c=radius/np.cos(theta)
temp_r = np.mod (r/radius_c, 1.0)
mask_1 = temp_r < 0.5
r = radius_c * (temp_r * 2 * mask_1 + (1-temp_r) * 2 * (1 - mask_1))
theta = theta + angle
x1_mask = r * np.cos(theta) + center_x
y1_mask = r * np.sin(theta) + center_y
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
img_out = img * 1.0
int_x = np.floor (x1_mask)
int_x = int_x.astype(int)
int_y = np.floor (y1_mask)
int_y = int_y.astype(int)
p_mask = x1_mask - int_x
q_mask = y1_mask - int_y
img_out = img * 1.0
for ii in range(row):
for jj in range (col):
new_xx = int_x [ii, jj]
new_yy = int_y [ii, jj]
# p = p_mask[ii, jj]
# q = q_mask[ii, jj]
img_out[ii, jj, :] = img[new_yy, new_xx, :]
plt.figure (1)
plt.imshow (img)
plt.axis('off')
plt.figure (2)
plt.imshow (img_out)
plt.axis('off')
plt.show()
附:PS 滤镜万花筒效果原理
clc;
clear all;
close all;
addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm');
I=imread('4.jpg');
I=double(I);
Image=I/255;
sz=size(Image);
% set the parameters
radius = 150;
angle = pi/4;
angle2=pi/4;
sides=10;
centerX = 0.5; % set the center of the circle, proportion of the image size
centerY = 0.5;
iWidth=sz(2);
iHeight=sz(1);
icenterX=iWidth*centerX;
icenterY=iHeight*centerY;
Image_new=Image;
for i=1:sz(1)
for j=1:sz(2)
dx=j-icenterX;
dy=i-icenterY;
r=sqrt(dy*dy+dx*dx);
theta=atan2(dy, dx)-angle-angle2;
temp_theta=theta/pi*sides*0.5 ;
theta=triangle(temp_theta);
if (radius)
c=cos(theta);
radius_c=radius/c;
r=radius_c * triangle(r/radius_c);
end
theta=theta+angle;
x=r * cos(theta)+icenterX;
y=r * sin(theta)+icenterY;
if (x<=1) x=1; end
if (x>=sz(2)) x=sz(2)-1; end;
if (y>=sz(1)) y=sz(1)-1; end;
if (y<1) y=1; end;
% % % if (x<=1) continue; end
% % % if (x>=sz(2)) continue; end;
% % % if (y>=sz(1)) 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程序设计有所帮助。
来源:http://blog.csdn.net/matrix_space/article/details/72303510
0
投稿
猜你喜欢
- 程序设计中三种基本机构是顺序结构、选择结构和循环结构。顺序结构语句是程序中最基础的语句,赋值语句、输入/输出语句、模块导入语句等都是顺序结构
- 简介显示大量已经按类别(不是很多)排序的数据但没有类别分界线,用户很难找到所需要的类别。例如,数据库中只有9个类别(8个不同的类别和1个nu
- 简介:设计稿尺寸标注与取色专用工具,适用于设计、界面开发与网页前端安装包仅700KB,全绿色独有的双模式切换可支持双屏显示器,一面设计,一面
- 如下所示:beta分布的最大特点是其多样性, 从下图可以看出, beta分布具有各种形态, 有U形, 类似正态分布的形状, 类似unifor
- 对于管理系统,常常需要展示列表数据,我们对于列表内的数据常常需要查找、过滤、排序等操作,其中查找等操作大部分是在后台进行的。django r
- 鼠标经过超链时的提示图层源码运行代码框<script>var oPopup = window.createPopup();fun
- gchart:基于google图表API的jquery组件全攻略——入门gchart是基于google图表API的jquery组件。使用gc
- 本文实例讲述了Python获取运行目录与当前脚本目录的方法。分享给大家供大家参考。具体实现方法如下:import osimport sys#
- 包括安装时提示有挂起的操作、收缩数据库、压缩数据库、转移数据库给新用户以已存在用户权限、检查备份集、修复数据库等。 (一)挂起操作在安装S
- 大家觉得在接手遗留代码时,见到什么东东是最让人感到不耐烦的?复杂无比的 UML ?我觉得不是。我的答案是,超过两个 else 的 if ,或
- 以前写的很简单,只有几句话,最近发现本文是本博客阅读量最大的一篇文章,觉得这样有种把人骗进来的感觉,于是又细化了一些。如果还有不好的地方,欢
- 当你要使用data URI scheme的时候,你会发现,虽然他可以使用在绝大多数浏览器上,但无法再IE6和IE7上工作。不过值得庆幸的这一
- python os.stat()获取相关文件的系统状态信息stat 系统调用时用来返回相关文件的系统状态信息的。下面直接以一个具体示例来进行
- 本文实例讲述了php实现的一段简单概率相关代码。分享给大家供大家参考,具体如下:<?phpfor($i=1;$i<100000;
- 如何在Access数据库中立即得到所插入记录的自动编号?首先,我们要保证获得记录集的方式支持bookmark属性,如在1、3插入一条有自动编
- 引言我们前面的文章介绍了数字和字符串,比如我计算今天一天的开销花了多少钱我可以用数字来表示,如果是整形用 int ,如果是小数用 float
- Cookies,有些人喜欢它们,有些人憎恨它们。但是,很少有人真正知道如何使用它们。现在你可以成为少数人中的成员-可以自傲的Cookie 大
- 作为胶水语言,Python可以很方便的执行系统命令,Python3中常用的执行操作系统命令有os.system()、os.popen()、s
- 目录创建文件夹布局编写命令代码实际应用场景案例1:检查数据库连接是否已就绪案例2:周期性发送邮件每次在启动Django服务之前,我们都会在终
- golang 字符串 int uint int64 uint64 互转字符串 转 intintNum, _ = strconv.Atoi(i