Allow saving some PA images as PNG - #9833
Conversation
| if index_alpha.setdefault(index, alpha_value) != alpha_value: | ||
| msg = ( | ||
| "cannot write mode PA as PNG: " | ||
| "a palette entry is used with multiple alpha values" |
There was a problem hiding this comment.
I'm not saying we need to go down this rabbit hole, but if there are less than 256 entries, it would be possible to achieve two alpha values for one palette entry by copying the entry and remapping it within the image. So a (255, 0, 0) entry with 100 alpha and 200 alpha could become two (255, 0, 0) entries with individual alphas.
There was a problem hiding this comment.
That could be a future enhancement, but it'd also mean we'd write a different palette than the user originally passed in into the PNG. (The roundtrip test here would probably fail.)
|
This would be the only conversion done when saving a single frame PNG. I wonder if it isn't better/consistent to require users to convert the image before the save? You're converting a PA image, to P with RGBA palette. Maybe a generic |
I thought about that too (or rather whether this function should belong somewhere else), and I'm not sure.
Anyway, while looking at this (comment!) I also found #9834... |
Limitation found while working on #9829: we weren't able to save PA (palette index + alpha band) images into PNG at all. This adds limited capacity for that: in case each index+alpha combination occurs only once across the image, we can save it as P+RGBA-palette instead.
An example PA file saved with Pillow - those are solid primaries! 😄