使用Python的pillow、numpy第三方库来实现图片转素描
安装依赖
1 2
| python3 -m pip install pillow python3 -m pip install numpy
|
完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import tkinter.filedialog
from PIL import Image import numpy as np
root = tkinter.Tk().withdraw()
file_ = tkinter.filedialog.askopenfilename()
a = np.asarray(Image.open( file_).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. 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 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)
b = 255 * (dx * uni_x + dy * uni_y + dz * uni_z) b = b.clip(0, 255)
im = Image.fromarray(b.astype('uint8')) im.save('素描.png')
|
效果图

