Skip to content

Conversation

@radarhere
Copy link
Member

@radarhere radarhere commented Jun 17, 2025

Resolves #9022

PNG images can only have a maximum of 16 bits per channel - https://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html

Bit depth is a single-byte integer giving the number of bits per sample or per palette index (not per pixel). Valid values are 1, 2, 4, 8, and 16, although not all values are allowed for all color types.

However, Pillow still allows saving PNG images as I mode, by clipping the data.

The issue has requested that we prevent this automatic transformation, so this PR deprecates it.

@radarhere radarhere added the Deprecation Feature that will be removed in the future label Jun 17, 2025

In order to fit the 32 bits of I mode images into PNG, when PNG images can only contain
at most 16 bits for a channel, Pillow has been clipping the values. Rather than quietly
changing the data, this is now deprecated.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we give advice on what people should do instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the commit to append

Instead, the image can be converted to another mode before saving:: from PIL import Image im = Image.new("I", (1, 1)) im.convert("I;16").save("out.png") 
@radarhere
Copy link
Member Author

radarhere commented Jun 18, 2025

I noticed that the deprecation warning wasn't being emitted for

from PIL import Image im = Image.new("I", (1, 1)) im.save("out.png")

This is because the call to deprecate() isn't in the function that the user calls (im.save()), it's one level deeper than that (PngImagePlugin._save). I've added a commit to pass a custom stacklevel to deprecate().

@hugovk hugovk merged commit 13faa46 into python-pillow:main Jun 21, 2025
77 of 78 checks passed
@radarhere radarhere deleted the png_i branch June 21, 2025 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Deprecation Feature that will be removed in the future

2 participants