First, let's take a look at the original picture:
Then we take a look at the renderings:
Through analysis, we can easily find the following characteristics:
The main color is black, white and gray
Heavier border lines
The same or similar colors tend to be white
Slight light source effect
The libraries that need to be used are:
numpy
PIL
Code:
importnumpyasnp
fromPILimportImage
baseImg=Image.open("./img/myimg2.jpg").convert("L")#Place the original picture you want to hand-paint here
a=np.array(baseImg).astype("float")
depth=8.
grad=np.gradient(a)#Take the gradient value of the image gray
grad_x,grad_y=grad#Simulate image gradient value
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#Light source top angle, radian value
vec_az=np.pi/4.#The official’s azimuth angle, radian value
dx=np.cos(vec_el)*np.cos(vec_az)#The influence of the light source on the x-axis
dy=np.sin(vec_el)*np.sin(vec_az)#The influence of light source on 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)#limit
img=Image.fromarray(b.astype("uint8"))#Reconstructed image
img.save("./toImg/myImage1.jpg")#Save image
Example extension:
from PIL import Image
import numpy as np
# In order to facilitate the import of files, you can use relative paths to place the files and programs in the same folder
vec_el=np.pi/2.2
vec_az=np.pi/4.
depth=10.
im=Image.open("HIT2.jpg").convert('L')
a=np.asarray(im).astype('float')
grad=np.gradient(a)
grad_x,grad_y=grad
grad_x=grad_x*depth/100.
grad_y=grad_y*depth/100.
dx=np.cos(vec_el)*np.cos(vec_az)
dy=np.cos(vec_el)*np.sin(vec_az)
dz=np.sin(vec_el)
A=np.sqrt(grad_x**2+grad_y**2+1.)
uni_x=grad_x/A
uni_y=grad_y/A
uni_z=1./A
a2=255*(dx*uni_x+dy*uni_y+dz*uni_z)
a2=a2.clip(0,255)
im2=Image.fromarray(a2.astype('uint8'))
im2.save('hit2-SH.jpg')
This is the end of this article about the sharing of hand drawing effects in Python. For more related Python hand drawing effects, please search for ZaLou.Cn's previous articles or continue to browse related articles below. Hope you will support ZaLou more in the future. .Cn!
Recommended Posts