Python實現(xiàn)PS濾鏡中馬賽克效果示例
本文實例講述了Python實現(xiàn)PS濾鏡中馬賽克效果。分享給大家供大家參考,具體如下:
這里利用 Python 實現(xiàn)PS 濾鏡中的馬賽克效果,具體的算法原理和效果可以參考附錄說明,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 : 利用鄰域的任意一點代替當前鄰域所有像素點 %%%% 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);
原圖
效果圖
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python圖片操作技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python Socket編程技巧總結》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
相關文章
python?selenium中Excel數(shù)據(jù)維護指南
這篇文章主要給大家介紹了關于python?selenium中Excel數(shù)據(jù)維護的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-03-03TensorFlow和Numpy矩陣操作中axis理解及axis=-1的解釋
在調用numpy庫中的concatenate()時,有遇到axis=-1/1/0的情況,下面這篇文章主要給大家介紹了關于TensorFlow和Numpy矩陣操作中axis理解及axis=-1解釋的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-03-03Python直接使用plot()函數(shù)畫圖的方法實例
Python非常簡單而又非常強大,它的功能之一就是畫出漂亮的圖表,實現(xiàn)數(shù)據(jù)的可視化,下面這篇文章主要給大家介紹了關于Python直接使用plot()函數(shù)畫圖的相關資料,需要的朋友可以參考下2022-05-05