[ACCEPTED]-PIL does not save transparency-python-imaging-library

Accepted answer
Score: 31

Probably the image is indexed (mode "P" in 12 PIL), so the transparency is not set in 11 PNG alpha channel, but in metadata info.

You 10 can get transparent background palette index 9 with the following code:

from PIL import Image

img = Image.open('1.png')
png_info = img.info
img.save('2.png', **png_info)

image info is a 8 dictionary, so you can inspect it to see 7 the info that it has:

eg: If you print it 6 you will get an output like the following:

{'transparency': 7, 'gamma': 0.45454, 'dpi': (72, 72)}

The 5 information saved there will vary depending 4 on the tool that created the original PNG, but 3 what is important for you here is the "transparency" key. In 2 the example it says that palette index "7" must 1 be treated as transparent.

Score: 6

You can always force the the type to "RGBA",

img = Image.open('1.png')
img.convert('RGBA')
img.save('2.png')

0

More Related questions