PIL Image mode “P” -> “RGBA”

我的未来我决定 提交于 2019-12-05 16:36:20

问题


This is my issue:

import Image
im = Image.open("1.png")
im.show()
print im.mode
im.convert("RGBA").save("2.png")

Well, with my image you can see the difference.
My question is: how do I convert it properly?

Image:

Result:

NOTE: The original image has a semi-transparent glow, the result has a solid green "glow"


回答1:


This issue was reported here:

https://bitbucket.org/effbot/pil-2009-raclette/issue/8/corrupting-images-in-palette-mode

In March 2012, a comment says it's now fixed in development version of PIL. The most recent released version is 1.1.7, so the fix won't be available until 1.2 comes out. PIL updates very slowly, so don't expect this to come out soon.




回答2:


Unfortunately your PNG image is a type that PIL doesn't handle very well - a paletted image with an alpha channel. When you open the image, the alpha is thrown away and there's no way to get it back.

This is different from the usual palette transparency where one index of the palette is used to denote fully transparent pixels.




回答3:


You could use scipy.misc.imread:

img = scipy.misc.imread(filename, mode='RGBA')
img = Image.fromarray(img)



回答4:


Your problem is that you do not provide info about what PIL should use as source of ALPHA channel.

PIL will not on its own add transparency to your image.

What part of your image you want to be transparent?



来源:https://stackoverflow.com/questions/12462548/pil-image-mode-p-rgba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!