【发布时间】:2020-09-16 00:26:04
【问题描述】:
我想写一个透明背景的PNG图像。 当我向数组添加 alpha 通道时。 plt.imsave 不起作用。让 red、green、blue 成为 float32 类型的 numpy 数组。
作品:
mask = red*green*blue
red[np.where(mask==0)]=0
green[np.where(mask==0)]=0
blue[np.where(mask==0)]=0
rgb = np.dstack((red,green,blue))
plt.imsave("sample.png", rgb, dpi = 300)
不工作:
mask = red*green*blue
red[np.where(mask==0)]=0
green[np.where(mask==0)]=0
blue[np.where(mask==0)]=0
alpha = np.where((mask==0), 0, 255).astype('float32')
rgba = np.dstack((red,green,blue, alpha))
plt.imsave("sample.png", rgba, dpi = 300)
当我添加 Alpha 通道时,plt.imsave 会停止工作。如何解决?
【问题讨论】:
-
你标记了
opencv,为什么不用cv2.imwrite()? -
我试过了,但是输出的是一个完整的 alpha 通道,或者只是一个透明的图像。它不会返回错误。
标签: python image matplotlib