Python PIL - 具有不透明度的 PNG 所有区域 > 0 将其不透明度设置为 1
想象一个带有黑色阴影的红色圆圈,在完全透明的背景上逐渐消失。当我使用 PIL 打开并重新保存图像时,背景保持完全透明,但阴影变为全黑。
甚至在不更改图像的情况下也会出现问题:
image = Image.open('input.png')
image = image.convert('RGBA')
image.save('output.png')
我想让图像看起来与原始图像完全相同,以便我可以裁剪或调整其大小。
编辑:这是一个演示效果的 PNG。使用 PNGNQ 将其转换为 8 位。
使用上述 Python 代码时,结果如下:
Imagine a red circle with a black dropshadow that fades away on top of a fully transparent background. When I open and resave the image with PIL the background remains fully transparent but the dropshadow becomes full black.
The problem appears without even altering the image:
image = Image.open('input.png')
image = image.convert('RGBA')
image.save('output.png')
I want to keep the image looking exactly as the original so that I can crop or resize it.
EDIT: Here's a PNG that demonstrates the effect. It was converted to 8bit by using PNGNQ.
When using the above Python code it comes out as the following:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来 PIL 目前不支持 PNG8 的完整 Alpha。
这里有一个用于只读支持的补丁: http:// mail.python.org/pipermail/image-sig/2010-October/006533.html
如果你觉得顽皮,你可以猴子补丁PIL:
It looks like PIL currently doesn't support full alpha for PNG8.
There is a patch here for read-only support: http://mail.python.org/pipermail/image-sig/2010-October/006533.html
If you're feeling naughty, you could monkeypatch PIL:
我认为问题已经得到了一定程度的解决,但是是否有可能需要设置 alpha 通道的深度?
I think that the problem has been somewhat resolved, but is it possible that you need to set the depth of the alpha channel?