【问题标题】:plt.imsave does not work when alpha channel is added添加 Alpha 通道时 plt.imsave 不起作用
【发布时间】: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


【解决方案1】:

这条线看起来不对:

alpha = np.where((mask==0), 0, 255).astype('float32')

不应该是这样吗:

alpha = np.where((mask==0), 0, 1).astype('float32')

alpha = np.where((mask==0), 0, 255).astype('uint8')

取决于dtypergb 频道。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-27
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    相关资源
    最近更新 更多