(Numpy+image) Get a picture with hand-drawn effect

(Numpy+image) Get a picture with hand-drawn effect


When studying data analysis recently, the teacher talked about a very interesting content of numpy, which is to modify the picture into a hand-painted picture. Not much nonsense, just go to the picture [doge].

Insert picture description here

Insert picture description here

I won’t talk about the principle, and I didn’t figure it out by myself. Just upload the code and watch the video if you’re interested.

import numpy as np
from PIL import Image

a = np.asarray(Image.open(r'C:\Users\pc\Pictures\Saved Pictures\桌面.jpg').convert(('L'))).astype('float')

depth = 10
grad = np.gradient(a)
grad_x, grad_y = grad
grad_x = grad_x*depth/100
grad_y = grad_y*depth/100

vec_el = np.pi/2.2    # 光源的俯视角度,弧度值
vec_az = np.pi/4.    # 光源的方位角度,弧度值
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.)
# 光源对x,y,z轴的影响
uni_x = grad_x/A
uni_y = grad_y/A
uni_z = 1./A

# 光源归一化
b = 255*(dx*uni_x + dy*uni_y + dz*uni_z)
b = b.clip(0,255)

# 重构图像并保存
im = Image.fromarray(b.astype('uint8'))
im.save(r'C:\Users\pc\Desktop\03.jpg')

Attach the video address

Guess you like

Origin blog.csdn.net/qq_43965708/article/details/109476182
Recommended