python - 添加 Alpha 通道时 plt.imsave 不起作用
问题描述
我想写一个透明背景的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)
plt.imsave 只是在我添加 Alpha 通道时停止工作。如何解决这个问题?
解决方案
这条线看起来不正确:
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')
取决于渠道dtype
。rgb
推荐阅读
- java - 这两个任务是同时执行的吗?
- wordpress - Calling variable inside loop based on post count
- google-maps - 谷歌地图位置更改和显示位置标记取决于用户选择的下拉值
- r - R App in Docker Container: Unable to download PDF report (Error: No such file or directory) Knitr/Rmarkdown
- python - 在python中停止谷歌语音到文本流识别客户端
- reactjs - 如何在heroku的客户端访问环境变量
- sql - Postgres - 需要 DB 设计和 Spring-Data-JPA 查询相关建议
- c - 这个 C 代码中的函数调用了多少次?
- mongodb - 在文档已嵌入文档的 MongoDB 查询中组合 $and 和 $or 条件
- r - 如何将数据框中的值从“xx个月”替换为“xx”