Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

Deprecate saving I mode images as PNG

Open radarhere opened this issue 6 months ago • 1 comments

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 avatar Jun 17 '25 08:06 radarhere

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().

radarhere avatar Jun 18 '25 12:06 radarhere