簡體   English   中英

添加 alpha 通道時 plt.imsave 不起作用

[英]plt.imsave does not work when alpha channel is added

我想寫一個透明背景的PNG圖像。 當我向數組添加一個 alpha 通道時。 plt.imsave將不起作用。 將紅色、綠色、藍色設為 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 停止工作。 如何解決這個問題?

這行看起來不對:

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')

取決於rgb通道的dtype

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM