Python implementation of hand drawing effect example sharing

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 libraries that need to be used are:

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

Python implementation of hand drawing effect example sharing
Python implementation of AI automatic matting example analysis
Python drawing rose implementation code
Python implementation of gomoku program
matplotlib of python drawing module
Detailed implementation of Python plug-in mechanism
Implementation of reverse traversal of python list
Python implementation of IOU calculation case
Python preliminary implementation of word2vec operation
Implementation of python selenium operation cookie
Python ATM function implementation code example
Implementation of python3 registration global hotkey
Example operation of python access Alipay
Implementation of python gradient descent algorithm
Free definition of python drawing legend
Basic analysis of Python turtle library implementation
Python implementation of intersection and IOU tutorial
Implementation principle of dynamic binding of Python classes
Example method of reading sql from python
python3 realizes the function of mask drawing
python drawing | IPCC-AR6 latest climate zone mask example
Implementation of Python headless crawler to download files
Implementation of business card management system with python
python drawing | IPCC-AR6 latest climate zone mask example
Example of feature extraction operation implemented in Python
Python writes the game implementation of fishing master
7 features of Python3.9
Implementation of business card management system based on python
Python drawing | A variety of typhoon path visualization methods
Detailed explanation of the implementation steps of Python interface development
Example of how to automatically download pictures in python