Python implements the method of PS image abstract painting effect, and python Painting Style

Source: Internet
Author: User

Python implements the method of PS image abstract painting effect, and python Painting Style

This article describes how to implement the abstract painting Effect of PS images in Python. We will share this with you for your reference. The details are as follows:

Today we will introduce a special effect on generating Abstract Image Based on Image Segmentation and color map random sampling. Simply put, the color is a color map, and the color is gradient, next, we split the image to be processed. Here we use the SLIC algorithm. Then we randomly sample the image from the color map and assign the sampled pixel value to the segmented image area.

# -*- coding: utf-8 -*-"""Created on Sun Aug 20 08:31:04 2017@author: shiyi"""import numpy as npimport matplotlib.pyplot as pltfrom skimage import iofrom skimage.segmentation import slicimport numpy.matlibimport randomfile_name='D:/Visual Effects/PS Algorithm/9.jpg';img=io.imread(file_name)row, col, channel = img.shape# define the colormapcolor_map = img.copy()rNW = 0.5rNE = 1.0rSW = 0.0rSE = 0.5gNW = 0.0gNE = 0.5gSW = 0.0gSE = 1.0bNW = 1.0bNE = 0.0bSW = 0.5bSE = 0.0xx = 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)fx = x_mask * 1.0 / colfy = y_mask * 1.0 / rowp = rNW + (rNE - rNW) * fxq = rSW + (rSE - rSW) * fxr = ( p + (q - p) * fy )r[r<0] = 0r[r>1] =1p = gNW + (gNE - gNW) * fxq = gSW + (gSE - gSW) * fxg = ( p + (q - p) * fy )g[g<0] = 0g[g>1] =1p = bNW + (bNE - bNW) * fxq = bSW + (bSE - bSW) * fxb = ( p + (q - p) * fy )b[b<0] = 0.0b[b>1] = 1.0color_map[:, :, 0] = r * 255color_map[:, :, 1] = g * 255color_map[:, :, 2] = b * 255# segment the imageN_block = 100segments = slic(img, n_segments=N_block, compactness=10)# plt.imshow(segments, plt.cm.gray)seg_img = img.copy()T_mask = img.copy()for i in range(N_block): mask = (segments == i) T_mask[:, :, 0] = mask T_mask[:, :, 1] = mask T_mask[:, :, 2] = mask x_ind = int(random.random() * (col-1)) y_ind = int(random.random() * (row-1)) color = color_map[y_ind, x_ind, :] T_img = seg_img * T_mask T_img = color seg_img = seg_img * (1-T_mask) + T_img * T_maskplt.figure(2)plt.imshow(seg_img)plt.show()

Source image:

:

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.