作者热门文章
- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# -*- coding:utf-8 -*-
import
cv2
import
numpy as np
from
tkinter
import
filedialog, Tk
from
os
import
getcwd
from
re
import
findall
def
open_path():
# 图片路径
root
=
Tk()
root.withdraw()
file_path
=
(filedialog.askopenfilename(title
=
'选择图片文件'
, filetypes
=
[(
'All Files'
,
'*'
)]))
return
file_path
def
dodgeNaive(image, mask):
# determine the shape of the input image
width, height
=
image.shape[:
2
]
# prepare output argument with same size as image
blend
=
np.zeros((width, height), np.uint8)
for
col
in
range
(width):
for
row
in
range
(height):
# do for every pixel
if
mask[col, row]
=
=
255
:
# avoid division by zero
blend[col, row]
=
255
else
:
# shift image pixel value by 8 bits
# divide by the inverse of the mask
tmp
=
(image[col, row] <<
8
)
/
(
255
-
mask)
# print('tmp={}'.format(tmp.shape))
# make sure resulting value stays within bounds
if
tmp.
any
() >
255
:
tmp
=
255
blend[col, row]
=
tmp
return
blend
def
dodgeV2(image, mask):
return
cv2.divide(image,
255
-
mask, scale
=
256
)
def
burnV2(image, mask):
return
255
-
cv2.divide(
255
-
image,
255
-
mask, scale
=
256
)
def
rgb_to_sketch(src_image_name):
print
(
'转换中......'
)
img_rgb
=
cv2.imread(src_image_name)
img_gray
=
cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
# 读取图片时直接转换操作
# img_gray = cv2.imread('example.jpg', cv2.IMREAD_GRAYSCALE)
img_gray_inv
=
255
-
img_gray
img_blur
=
cv2.GaussianBlur(img_gray_inv, ksize
=
(
21
,
21
),
sigmaX
=
0
, sigmaY
=
0
)
img_blend
=
dodgeV2(img_gray, img_blur)
# cv2.imshow('original', img_rgb)
# cv2.imshow('gray', img_gray)
# cv2.imshow('gray_inv', img_gray_inv)
# cv2.imshow('gray_blur', img_blur)
cv2.imwrite(dst_image_name, img_blend)
save_path
=
getcwd()
+
"\\"
+
dst_image_name
# 保存路径
print
(
'转换完成!!!\n'
)
print
(
'保存路径:'
+
save_path)
cv2.imshow(save_path, img_blend)
cv2.waitKey(
0
)
cv2.destroyAllWindows()
if
__name__
=
=
'__main__'
:
print
(
'请选择图片(路径不要含中文):'
)
src_image_name
=
open_path()
# 文件路径
print
(src_image_name
+
'\n'
)
image_name
=
'
'.join(findall(r'
[^\\
/
:
*
?"<>|\r\n]
+
$', src_image_name))
# 获取文件名
dst_image_name
=
'Sketch_'
+
image_name
rgb_to_sketch(src_image_name)
|
效果如下 。
以上就是python如何将图片转换素描画的详细内容,更多关于python图片转换素描画的资料请关注我其它相关文章! 。
原文链接:https://cloud.tencent.com/developer/article/1640047 。
最后此篇关于python如何将图片转换素描画的文章就讲到这里了,如果你想了解更多关于python如何将图片转换素描画的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我是一名优秀的程序员,十分优秀!