-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
-
Right now trying to save an image with an RGBA palette as PNG raises an exception
from PIL import Image import numpy as np palette = np.random.random_integers(0, 255, size=(16,4)).astype(np.uint8) indices = np.random.random_integers(0, 16, size=(20, 10)).astype(np.uint16) img = Image.fromarray(indices, mode='PA') img.putpalette(palette) img.save("test.png")
OSError: cannot write mode PA as PNG
But at least Wikipedia states this should be possible:
With indexed color images, the palette always stores trichromatic colors at a depth of 8 bits per channel (24 bits per palette entry). Additionally, an optional list of 8-bit alpha values for the palette entries may be included;
Beta Was this translation helpful? Give feedback.
All reactions
I think this comes back to the same distinction as #9242 - PA images store a palette index and an independent alpha value for each pixel.
Your highlighted quote says, emphasis mine,
an optional list of 8-bit alpha values for the palette entries
What I think your highlighted quote is describing is P mode image with an RGBA palette, where the alpha values are for the palette entries, not for the pixel values. So if I had a palette with only two entries, (255, 0, 0, 127) and (0, 255, 0, 255), I must choose between translucent red and opaque green. With just those entries, I can't have opaque red.
That type of image can be saved as a PNG image with Pillow.
from PIL import Image import numpy as
Replies: 1 comment 1 reply
-
I think this comes back to the same distinction as #9242 - PA images store a palette index and an independent alpha value for each pixel.
Your highlighted quote says, emphasis mine,
an optional list of 8-bit alpha values for the palette entries
What I think your highlighted quote is describing is P mode image with an RGBA palette, where the alpha values are for the palette entries, not for the pixel values. So if I had a palette with only two entries, (255, 0, 0, 127) and (0, 255, 0, 255), I must choose between translucent red and opaque green. With just those entries, I can't have opaque red.
That type of image can be saved as a PNG image with Pillow.
from PIL import Image import numpy as np palette = np.random.random_integers(0, 255, size=(16,4)).astype(np.uint8) indices = np.random.random_integers(0, 16, size=(20, 10)).astype(np.uint16) img = Image.fromarray(indices, mode='P') img.putpalette(palette, 'RGBA') img.save("test.png")
If you're interested in evidence from the specification,
Screenshot 2025年10月12日 at 1 41 52 pm
Screenshot 2025年10月12日 at 2 00 57 pm
Screenshot 2025年10月12日 at 2 03 34 pm
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
Thank you for the explanation. Indeed the difference between RGBA 'P' and 'PA' images was unclear to me.
Beta Was this translation helpful? Give feedback.