Skip to content

Keep high bits when saving L images with fewer PNG bits - #10708

Open
guru-bharadwaj20 wants to merge 2 commits into
python-pillow:mainfrom
guru-bharadwaj20:png-l-bits-high
Open

guru-bharadwaj20 wants to merge 2 commits into
python-pillow:mainfrom
guru-bharadwaj20:png-l-bits-high

Conversation

@guru-bharadwaj20

Copy link
Copy Markdown
Contributor

Saving an L image as PNG with bits=1, 2 or 4 keeps the low bits of each pixel, but reading the file back treats the stored values as the high bits. So the round trip changes the image:

from PIL import Image

im = Image.new("L", (6, 1))
im.putdata([0, 17, 58, 128, 200, 255])
im.save("out.png", bits=4)
with Image.open("out.png") as reloaded:
    print(reloaded.get_flattened_data())
# main:          (0, 17, 170, 0, 136, 255)
# this branch:   (0, 17, 51, 136, 204, 255)

The cause is in Pack.c. The L;4 unpacker reads each nibble as the high bits (n * 17), but packL4 and packL2 keep x & 15 and x & 3, and L;1 uses pack1, which sets a bit for any non-zero pixel. The PNG bits option for L just picks the L;1, L;2 or L;4 rawmode, so it inherits the mismatch.

Changes proposed in this pull request:

  • Add packL1, packL2 and packL4, which keep the highest 1, 2 or 4 bits of each pixel, and use them for the L rawmodes. The old low-bit functions are renamed to packP2 and packP4 and stay on the P rawmodes, where the low bits are the palette index.
  • PalmImagePlugin was the other caller of these rawmodes, and it shifted values into the low bits before packing. Both of its L paths now produce values in the high bits instead. I saved hopper, a gradient and a random image at bpp 1, 2 and 4 through both paths (plus the 1 and P modes), and the output bytes are identical to main for all 20 cases.
  • An integer transparency for an L image saved with fewer bits is shifted the same way, so the tRNS value still matches the pixels it was meant for.
  • Pack tests for L;1, L;2 and L;4 using the same bytes as the existing unpack tests, PNG round trip tests with exact pixel values for bits 1, 2 and 4, a transparency test, and a release note.

Checks run locally (Linux, Python 3.14):

  • python3 -m pytest Tests/test_lib_pack.py Tests/test_file_png.py Tests/test_file_palm.py Tests/test_image_point.py: 166 passed, 8 skipped (the Palm tests need ImageMagick, which isn't installed here, so I compared the Palm output bytes directly as described above).
  • With the src/ changes reverted, the new test_L pack test and the 4 new PNG tests fail.
  • pre-commit run on the changed files passes.

tobytes("raw", "L;4") and the other L rawmodes now give the high bits too, which the release note mentions.

The L;1, L;2 and L;4 packers kept the low bits of each pixel, while the unpackers read them as the high bits, so a PNG saved with bits=4 did not read back as the same image. Pack the high bits instead, adjust the Palm plugin so its output is unchanged, and reduce PNG transparency the same way.
Copilot AI balanced review requested due to automatic review settings December 3, 2026 05:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@radarhere radarhere added the 🤖-assisted AI-assisted label Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖-assisted AI-assisted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants