【发布时间】:2021-02-08 08:12:04
【问题描述】:
我正在制作一个绘图程序,您可以稍后将更改保存并导出为图像。不透明图像选项可以完美运行,但透明选项不能。我现在的代码基于this post。
每当我在透明选项中画一条线时,图像上都不会显示任何内容。它是完全透明的。
print("Transparent:", str(transparent))
if not transparent:
image = np.zeros((height, width, 3), np.uint8) # initialize image for saving preferences
image[:] = backgroundColorBGR # change background color
for drawing in drawings: # loop through each drawing
cv2.line(image, drawing[0], drawing[1], drawing[2], thickness = drawing[3]) # create line that user drew
cv2.imwrite("yourimage.png", image)
else:
image = np.zeros((height, width, 4), dtype = np.uint8) # 4 channels instead of 3, for transparent images
for drawing in drawings: # loop through each drawing
cv2.line(image, drawing[0], drawing[1], drawing[2], thickness = drawing[3])
cv2.imwrite("yourimage.png", image)
【问题讨论】:
-
尝试将
color=(b,g,r,a)添加到cv2.line()的参数中。我的意思是特别是 4 个值,所以你包括透明度/alpha。使a等于255。或者尝试拆分通道并将前3 个绘制为BGR,将最后一个绘制为灰度,然后与np.dstack()重新组合。 -
非常感谢您的帮助!请将此作为答案,因为我知道这会对其他人有所帮助。
-
我没有测试过这个,所以我不愿意把它作为答案。如果您成功了,请成为我的客人并将您的代码添加为答案,接受您自己的答案并获得积分。祝你好运!
标签: python opencv image-processing transparency