核心功能 - 自动生成素描草图

这个脚本可以把彩色图片转化为铅笔素描草图,对人像、景色都有很好的效果。

而且只需几行代码就可以一键生成,适合批量操作,非常的快捷。

需要的第三方库:

Opencv - 计算机视觉工具,可以实现多元化的图像视频处理,有Python接口

实现代码

 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66


""" Photo Sketching Using Python """


import cv2


img = cv2.imread("elon.jpg")





## Image to Gray Image


gray_image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)





## Gray Image to Inverted Gray Image


inverted_gray_image = 255-gray_image





## Blurring The Inverted Gray Image


blurred_inverted_gray_image = cv2.GaussianBlur(inverted_gray_image, (19,19),0)





## Inverting the blurred image


inverted_blurred_image = 255-blurred_inverted_gray_image





### Preparing Photo sketching


sketck = cv2.divide(gray_image, inverted_blurred_image,scale= 256.0)





cv2.imshow("Original Image",img)


cv2.imshow("Pencil Sketch", sketck)


cv2.waitKey(0)

效果展示