Example of the mosaic effect in the ps filter implemented by Python, python Filter
This article describes how to implement mosaic in the PS filter using Python. We will share this with you for your reference. The details are as follows:
Here, we use Python to implement the mosaic effect in the ps filter. For detailed algorithm principles and effects, refer to the appendix description. The Python sample code is as follows:
from skimage import img_as_floatimport matplotlib.pyplot as pltfrom skimage import ioimport randomimport numpy as npfile_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.shapehalf_patch =10for 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()
Appendix: Principle of the PS filter algorithm-Mosaic
% Method: Use any point in the neighborhood to replace all pixels in the current neighborhood % mosaicclc; 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; % controls the Neighborhood size Image_out = Image; for I = 1 + N: height-N for j = 1 + 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); endendimshow (Image_out/255 );
Source image