Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support paletted PNG with alpha #9243

Answered by radarhere
pekkavaa asked this question in Q&A
Discussion options

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;

You must be logged in to vote

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

Comment options

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

You must be logged in to vote
1 reply
Comment options

Thank you for the explanation. Indeed the difference between RGBA 'P' and 'PA' images was unclear to me.

Answer selected by radarhere
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /