Skip to content

Update transparency keys when posterizing images - #10728

Closed
euno102 wants to merge 1 commit into
python-pillow:mainfrom
euno102:codex/posterize-transparency
Closed

euno102 wants to merge 1 commit into
python-pillow:mainfrom
euno102:codex/posterize-transparency

Conversation

@euno102

@euno102 euno102 commented Dec 4, 2026

Copy link
Copy Markdown

Changes proposed in this pull request:

  • Apply the posterize lookup table to the transparency key of L and RGB images. Before this change the pixels changed, but the key still held the original color.
  • Reuse the transparency remapping from ImageOps.invert() (Update 1, L and RGB info transparency when inverting images #10722), preserving palette-based transparency and the output mode.
  • Add grayscale and RGB regression tests and a release note.

For example, posterizing an RGB pixel keyed as (250, 10, 10) to four bits yields (240, 0, 0). The output now uses that value as the transparency key. If colors map to the same value, they share the resulting key.

Validation on Linux / Python 3.12: selftest (59 passed), ImageOps tests (78 passed), and the full test suite (6,082 passed, 225 skipped, 3 xfailed). Both regression cases failed before the change. Applicable pre-commit hooks passed for the changed files.

AI disclosure: OpenAI Codex helped investigate, implement, and test the change and draft this description.

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

Copy link
Copy Markdown
Member

If colors map to the same value, they share the resulting key.

This seems like a problem to me. If there are two similar colours in the original image, but one has transparency, I think making them both have transparency after applying posterize() could be unexpected.

One alternative could be to raise an error if there is transparency.
Another would be to convert the image and return an RGBA image, so that the transparent pixels in the original image start transparent afterwards. However, it seems possible that for users that are posterizing their images, they may not want an RGBA image afterwards.

I don't think Pillow is behaving perfectly in this scenario, but I'd like to hear from someone who is actually using this functionality before making a decision.

It also seems like you would take issues with im.point(), which is what ImageOps calls.

@radarhere radarhere closed this Dec 4, 2026
@euno102

euno102 commented Dec 4, 2026 •

Copy link
Copy Markdown
Author

Thanks for pointing out the color collision. If an opaque pixel and a transparent pixel map to the same posterized color, updating the color key would make both pixels transparent. Returning RGBA could preserve the per-pixel alpha, but it would change the output mode callers receive.

@euno102
euno102 deleted the codex/posterize-transparency branch December 4, 2026 07:27
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.

2 participants