Skip to content

Apply invert, posterize and solarize to the palette of a P image - #10174

Open
RavSinghChandan wants to merge 4 commits into
python-pillow:mainfrom
RavSinghChandan:imageops-palette-lut
Open

RavSinghChandan wants to merge 4 commits into
python-pillow:mainfrom
RavSinghChandan:imageops-palette-lut

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

Changes proposed in this pull request:

  • ImageOps.invert(), posterize() and solarize() raised NotImplementedError: mode P support coming soon for a P image, from the FIXME in _lut(): "apply to lookup table, not image data".
  • These three change each pixel on its own, so for a P image the lookup table can be applied to the palette colors instead. The result is the same as applying it to the RGB image, and alpha in an RGBA palette is left unchanged.
  • autocontrast() and equalize() build their table from the histogram, which for a P image counts palette indexes rather than colors, so they still raise as before.
  • Tests check each operation against its RGB result, that palette alpha is kept, and that autocontrast still refuses P.

@radarhere

radarhere commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

equalize() build their table from the histogram, which for a P image counts palette indexes rather than colors, so they still raise as before.

_lut() doesn't actually need to be concerned about this, as equalize() converts P to RGB by itself.

Pillow/src/PIL/ImageOps.py

Lines 495 to 496 in b01a706

if image.mode == "P":
image = image.convert("RGB")

@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

You're right, thanks, my description was wrong about equalize(). The guard only matters for autocontrast(), which builds its table from image.histogram(), and for a P image that counts palette indexes. Your version looks good to me.

@radarhere radarhere added the 🤖-assisted AI-assisted label Oct 11, 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.

2 participants