【发布时间】:2017-07-07 22:39:55
【问题描述】:
我想在将图像保存为 png 文件之前更改图像的背景并为其添加 alpha 通道。
imshow 显示图像,但imwrite 写入空图像。
合并后的尺寸也是正确的,即当我打印img_a.shape时,合并后的图像有(x,y,4)
图像深度为uint8。我尝试将其更改为float32,然后除以255,但似乎没有任何效果。我缺少一些基本的东西。
我应该怎么做才能让imwrite 用 alpha 通道写入正确的 png?
我试过cv2.merge 和np.dstack。 imwrite 写入失败。
在用 gimp 打开它时,它显示了一层。
以下是我的代码。
imgo = cv2.imread('PCP_1.jpg')
image = cv2.GaussianBlur(imgo, (5, 5), 0)
r = image.shape[0]
c = image.shape[1]
shp = (r,c,1)
c_red, c_green, c_blue = cv2.split(image)
#c_red = c_red.astype(np.float32)
#c_green =c_green.astype(np.float32)
#c_blue = c_blue.astype(np.float32)
alphachn = np.zeros(shp)
#alphachn = alphachn.astype(np.float32)
img_a = cv2.merge((c_red, c_green, c_blue, alphachn))
#img_a = np.dstack( (imgo, np.zeros(shp).astype(np.uint8) ) )
print img_a.shape
cv2.imshow('image', img_a)
cv2.imwrite('image_alpha.png', img_a)
k = cv2.waitKey(0)
【问题讨论】:
标签: image python-2.7 opencv numpy