Skip to content

Fix saving L images with bpp as Palm - #10266

Merged
radarhere merged 3 commits into
python-pillow:mainfrom
qodelicious:fix-palm-l-save
Oct 22, 2026
Merged

radarhere merged 3 commits into
python-pillow:mainfrom
qodelicious:fix-palm-l-save

Conversation

@qodelicious

Copy link
Copy Markdown
Contributor

Saving an L image as Palm fails right now.

from PIL import Image
Image.linear_gradient("L").save("out.palm", bpp=4)
# ValueError: image has no palette

The L branch switches im._mode to "P" so the P;1, P;2 or P;4 rawmode can be used, and since #8685 the colormap code checks im.mode == "P", so it also runs for these images and calls getpalette() on an image that has none. The same happens when bpp comes from im.info.

Changes proposed in this pull request:

  • Only write the custom colormap when saving a real P image (rawmode == "P"). Grayscale Palm images don't use one, which matches the "we ignore the palette here" comment.
  • Added tests that check the header and the packed pixel bytes for bpp 1, 2 and 4, and for info["bpp"]
  • Documented the bpp save option and which modes can be saved, since it wasn't in the handbook

I also converted the bpp 1, 2 and 4 output of hopper with ImageMagick and it matched the expected gray levels exactly. Palm tests pass and ruff, black and sphinx-lint are happy.

qodelicious and others added 2 commits October 21, 2026 00:21
Since the colormap change in python-pillow#8685, an L image saved with bpp went through the P mode branch because its mode is switched to P before writing, and failed with 'image has no palette'. Only write the custom colormap for real P images. Also document the bpp option.
@radarhere
radarhere force-pushed the fix-palm-l-save branch 2 times, most recently from 55fc81b to 855003b Compare October 21, 2026 02:03
@radarhere

radarhere commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

The L branch switches im._mode to "P"

I actually think that is a problem. Saving an image shouldn't change it like that. I've pushed a commit to keep it in L mode, and fix the rawmode instead.

I'm not a huge fan of testing specific bytes in the saved image. If we're writing specific bytes, and then checking those values specifically again, it doesn't seem like an effective way to know if we did anything incorrect. Instead, I've pushed a commit to check the image mode and similarity to the original image. See what you think.

@radarhere radarhere changed the title Fix saving L images as Palm Fix saving L images with bpp as Palm Oct 21, 2026
@radarhere
radarhere merged commit 0c8cc31 into python-pillow:main Oct 22, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants