Python实现PS滤镜的万花筒效果示例
作者:Matrix_11 时间:2023-11-15 10:17:25
本文实例讲述了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
标签:Python,PS滤镜,万花筒
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
CSS的优先级与特殊性
2008-06-24 11:36:00
Vue中金额、日期格式化插件@formatjs/intl的使用及说明
2024-04-27 16:06:48
numpy和pandas中数组的合并、拉直和重塑实例
2022-06-28 02:55:07
一篇文章弄懂Python中所有数组数据类型
2023-01-12 18:25:05
pandas数据合并之pd.concat()用法详解
2022-10-26 20:22:38
Laravel框架实现点播上传阿里云功能
2023-06-13 20:13:30
浅谈python print(xx, flush = True) 全网最清晰的解释
2022-01-28 21:45:48
![](https://img.aspxhome.com/file/2023/7/76737_0s.jpg)
MySQL创建新用户、增加账户的2种方法及使用实例
2024-01-14 12:54:55
python制作机器人的实现方法
2022-11-09 21:09:05
![](https://img.aspxhome.com/file/2023/5/134935_0s.png)
Python实现读取Properties配置文件的方法
2021-01-05 17:28:21
合理设置内存让数据库与其他程序共存
2009-05-21 16:24:00
![](https://img.aspxhome.com/file/UploadPic/20095/2009521182025312.jpg)
使用python实现ANN
2022-05-30 09:56:42
![](https://img.aspxhome.com/file/2023/6/134696_0s.jpg)
我的ImageMagick使用心得
2008-10-21 11:05:00
![](https://img.aspxhome.com/file/UploadPic/200810/21/20081021122020655s.jpg)
深度剖析Golang中的数组,字符串和切片
2024-02-17 08:53:13
老生常谈CSS网页布局的意义与副作用
2008-09-12 12:31:00
Python NumPy库安装使用笔记
2022-05-20 17:49:56
Java使用JDBC向MySQL数据库批次插入10W条数据(测试效率)
2024-01-15 15:41:11
vue中父子组件传值,解决钩子函数mounted只运行一次的操作
2024-05-09 15:26:31
Python数据分析之 Pandas Dataframe修改和删除及查询操作
2023-08-03 23:01:13
![](https://img.aspxhome.com/file/2023/9/61699_0s.png)
django配置app中的静态文件步骤
2021-03-15 21:43:57