使用 PIL 将灰度 png 转换为透明度

发布于 2024-12-21 17:22:14 字数 406 浏览 8 评论 0原文

如果我将 png 图像设为灰度,PIL 会损坏具有透明度的 png 图像。为什么?

这是我的代码:

input = Image.open('input.png')
output = ImageOps.grayscale(input)
output.save('output.png', **input.info)

输入

http://imgur.com/a/m50p6

输出

http://imgur.com/a/m50p6

有办法解决这个问题吗?

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

http://imgur.com/a/m50p6

Output

http://imgur.com/a/m50p6

is there a way to fix that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗弃M 2024-12-28 17:22:14

您可以将 convert 方法与亮度技巧结合使用:

Image.open('input.png').convert('LA').save('output.png')

You can use convert method with luminance trick:

Image.open('input.png').convert('LA').save('output.png')
夜司空 2024-12-28 17:22:14

我也遇到了这个问题。我能找到的唯一解决方案是转换为“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文