from PIL import Image img = Image.open('1.png') img.save('2.png')
The first image has a transparent background, but when I save it, the transparency is gone (background is white)
What am I doing wrong?
Probably the image is indexed (mode "P" in PIL), so the transparency is not set in PNG alpha channel, but in metadata info.
You can get transparent background palette index with the following code:
image info is a dictionary, so you can inspect it to see the info that it has:
eg: If you print it you will get an output like the following:
The information saved there will vary depending on the tool that created the original PNG, but what is important for you here is the "transparency" key. In the example it says that palette index "7" must be treated as transparent.
You can always force the the type to "RGBA",