Python implements hand-drawn function

Python implements hand-drawn function

The hand-drawn drawings are indeed very good-looking, but for the handicapped, I can only smile. Today, I will use the code to realize the hand-drawn function and convert ordinary pictures into hand-drawn pictures.

Code

from PIL import Image
import numpy as np

a = np.asarray(Image.open(r'C:\Users\luopan\Desktop\1.jpg').convert('L')).astype('float')

depth = 10. # (0-100)
grad = np.gradient(a) # Take the gradient value of the image gray
grad_x, grad_y = grad # Take the horizontal and vertical image gradient values ​​respectively
grad_x = grad_x * depth/100.
grad_y = grad_y * depth/100.
A = np.sqrt(grad_x ** 2 + grad_y ** 2 + 1.)
uni_x = grad_x/A
uni_y = grad_y/A
uni_z = 1./A

vec_el = np.pi/2.2 # The overhead angle of the light source, radian value
vec_az = np.pi/4. # The azimuth angle of the light source, radian value
dx = np.cos(vec_el) * np.cos(vec_az) # The influence of the light source on the x axis
dy = np.cos(vec_el) * np.sin(vec_az) # The influence of the light source on the y axis
dz = np.sin(vec_el) # The influence of the light source on the z axis

b = 255 * (dx * uni_x + dy * uni_y + dz * uni_z) # light source normalization
b = b.clip(0, 255)

im = Image.fromarray(b.astype('uint8')) # reconstruct image
im.save(r'C:\Users\luopan\Desktop\2.jpg')

effect

This code is provided by the university's mooc python data analysis and display , only for learning use.

Reference: https://cloud.tencent.com/developer/article/1394180 Python implements hand-painted functions-Cloud + Community-Tencent Cloud