Example of the mosaic effect in the ps filter implemented by Python, python Filter

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.