使用 PIL 将灰度 png 转换为透明度
如果我将 png 图像设为灰度,PIL 会损坏具有透明度的 png 图像。为什么?
这是我的代码:
input = Image.open('input.png')
output = ImageOps.grayscale(input)
output.save('output.png', **input.info)
输入
输出
有办法解决这个问题吗?
PIL corrupt png images with transparency if i make them grayscale. Why?
Here's my code:
input = Image.open('input.png')
output = ImageOps.grayscale(input)
output.save('output.png', **input.info)
Input
Output
is there a way to fix that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
convert
方法与亮度技巧结合使用:You can use
convert
method with luminance trick:我也遇到了这个问题。我能找到的唯一解决方案是转换为“LA”,然后返回“RGBA”
尝试:
Image.open('input.png').convert('LA').convert(' RGBA')
我试图在 tkinter 画布上以透明度显示生成的灰度 PNG,但我认为此方法也可能适用于保存输出。
I ran into this issue as well. The only solution I've been able to find is to convert to 'LA' and then back to 'RGBA'
Try:
Image.open('input.png').convert('LA').convert('RGBA')
I was attempting to display the resulting grayscale PNG with transparency on a tkinter canvas, but I think this method will probably also work for saving the output.