Python實(shí)現(xiàn)PS濾鏡中馬賽克效果示例
本文實(shí)例講述了Python實(shí)現(xiàn)PS濾鏡中馬賽克效果。分享給大家供大家參考,具體如下:
這里利用 Python 實(shí)現(xiàn)PS 濾鏡中的馬賽克效果,具體的算法原理和效果可以參考附錄說(shuō)明,Python示例代碼如下:
from skimage import img_as_float
import matplotlib.pyplot as plt
from skimage import io
import random
import numpy as np
file_name='D:/Visual Effects/PS Algorithm/4.jpg';
img=io.imread(file_name)
img = img_as_float(img)
img_out = img.copy()
row, col, channel = img.shape
half_patch =10
for i in range(half_patch, row-1-half_patch, half_patch):
for j in range (half_patch, col-1-half_patch, half_patch):
k1 = random.random() - 0.5
k2 = random.random() - 0.5
m=np.floor(k1*(half_patch*2 + 1))
n=np.floor(k2*(half_patch*2 + 1))
h=int((i+m) % row)
w=int((j+n) % col)
img_out[i-half_patch:i+half_patch, j-half_patch:j+half_patch, :] =\
img[h, w, :]
plt.figure(1)
plt.imshow(img)
plt.axis('off')
plt.figure(2)
plt.imshow(img_out)
plt.axis('off')
plt.show()
附:PS 濾鏡算法原理 ——馬賽克
% method : 利用鄰域的任意一點(diǎn)代替當(dāng)前鄰域所有像素點(diǎn)
%%%% mosaic
clc;
clear all;
addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm');
Image=imread('4.jpg');
Image=double(Image);
size_info=size(Image);
height=size_info(1);
width=size_info(2);
N=11; % 控制鄰域大小
Image_out=Image;
for i=1+N:N:height-N
for j=1+N:N:width-N
k1=rand()-0.5;
k2=rand()-0.5;
m=(k1*(N*2-1));
n=(k2*(N*2-1));
h=floor(mod(i+m,height));
w=floor(mod(j+n,width));
if w==0;
w=width;
end
if h==0
h=height;
end
Image_out(i-N:i+N,j-N:j+N,1)=Image(h,w,1);
Image_out(i-N:i+N,j-N:j+N,2)=Image(h,w,2);
Image_out(i-N:i+N,j-N:j+N,3)=Image(h,w,3);
end
end
imshow(Image_out/255);
原圖

效果圖

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python 實(shí)現(xiàn)PS濾鏡的旋渦特效
- Python 實(shí)現(xiàn)PS濾鏡中的徑向模糊特效
- Python實(shí)現(xiàn)PS濾鏡特效Marble Filter玻璃條紋扭曲效果示例
- Python實(shí)現(xiàn)PS濾鏡Fish lens圖像扭曲效果示例
- Python實(shí)現(xiàn)PS濾鏡特效之扇形變換效果示例
- Python實(shí)現(xiàn)PS濾鏡功能之波浪特效示例
- Python實(shí)現(xiàn)PS濾鏡碎片特效功能示例
- Python實(shí)現(xiàn)PS濾鏡的萬(wàn)花筒效果示例
- Python實(shí)現(xiàn)PS濾鏡的旋轉(zhuǎn)模糊功能示例
- Python實(shí)現(xiàn)PS濾鏡中的USM銳化效果
相關(guān)文章
TensorFlow入門使用 tf.train.Saver()保存模型
這篇文章主要介紹了TensorFlow入門使用 tf.train.Saver()保存模型,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
基于python調(diào)用jenkins-cli實(shí)現(xiàn)快速發(fā)布
這篇文章主要介紹了基于python調(diào)用jenkins-cli實(shí)現(xiàn)快速發(fā)布,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
matplotlib.pyplot畫圖 圖片的二進(jìn)制流的獲取方法
今天小編就為大家分享一篇matplotlib.pyplot畫圖 圖片的二進(jìn)制流的獲取方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
python virtualenv虛擬環(huán)境配置與使用教程詳解
這篇文章主要介紹了python virtualenv虛擬環(huán)境配置與使用教程詳解,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
python使用json序列化datetime類型實(shí)例解析
這篇文章主要介紹了python使用json序列化datetime類型實(shí)例解析,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Python第三方庫(kù)jieba庫(kù)與中文分詞全面詳解
jieba庫(kù)是一款優(yōu)秀的Python第三方中文分詞庫(kù),jieba支持三種分詞模式:精確模式、全模式和搜索引擎模式,下面這篇文章主要給大家介紹了關(guān)于Python第三方庫(kù)jieba庫(kù)與中文分詞的相關(guān)資料,需要的朋友可以參考下2022-07-07
Python中turtle.write方法使用說(shuō)明
turtle模塊以面向?qū)ο蠛兔嫦蜻^(guò)程的方式提供turtle圖形基元,由于它使用Tkinter作為基礎(chǔ)圖形,因此需要安裝有Tk支持的Python版本,下面這篇文章主要給大家介紹了關(guān)于Python中turtle.write方法使用說(shuō)明的相關(guān)資料,需要的朋友可以參考下2022-02-02

