New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transparency is not applied to RGBA palette when opening PNG #6348
Comments
Hi. As you have said, the transparency information is in the info dictionary, but not in the palette. I'm not convinced this is a bug. I don't think it should be in both, but in one or the other. I don't like the idea of having two sources of truth about what the transparency is. The image is being opened with an RGB palette. When you are requesting an RGBA palette, the RGB palette is just being converted. If I ask Pillow to open and re-save test_A.png, test_B.png and test_C.png, the transparency is retained, so there's no information loss occurring. If you need the transparency index to become part of the palette for reasons external to Pillow, it should be straightforward for you to apply it in your code. |
I do actually have the code to apply transparency from before Pillow supported RGBA palettes, I was hoping to remove it. My reasoning is:
Obviously one problem is existing code that expects transparency in the info dictionary. But that code probably uses an RGB palette as well. |
How about this idea - rather than changing the default behaviour, what if we added an |
I would expect this to be the default if I request an RGBA palette over an RGB palette, also another argument will change the API which may cause problems in the future if you want to change it. |
Pillow values backwards compatibility, and this is not explicitly a bug - at the moment, I think |
What about a separate "expand palette" function, that internally updates the palette from RGB to RGBA and applies transparency. It would do nothing if the palette is already RGBA. |
I see, one function that takes an image with an RGB palette and "transparency" info and converts it to image with an RGBA palette without "transparency" info. I've created PR #6352 for this. Your second idea is a function that turns an image with an RGBA palette without "transparency" info into an image with an RGB palette and "transparency" info. Why would you consider this to be helpful? |
Only for symmetry, and you may have the function already for saving, so it would just be some refactoring. |
PngImagePlugin achieves this fairly neatly when saving Pillow/src/PIL/PngImagePlugin.py Lines 1340 to 1343 in 418820a
If it's not something helpful to users, I'm inclined not to make that operation part of the public API. |
What did you do?
I need to get a palette with transparency information from a PNG image. When saving this is correctly applied to the output image, when loading only an opaque palette is returned.
What did you expect to happen?
I expect that image.info["transparency"] is applied to a returned RGBA palette, i.e. alpha cleared for a single transparent colour, or alpha replaced with the array of transparency values.
What actually happened?
A completely opaque palette is returned
What are your OS, Python and Pillow versions?
This sets the transparency in 3 different ways and saves images to the current directory, then loads them back.
The images show the expected transparency if opened in a program like GIMP.
Output is:
Expected output should be something like:
The text was updated successfully, but these errors were encountered: