Skip to content

Add support for ImageOps invert, posterize and solarize of P images - #10174

Merged
radarhere merged 5 commits into
python-pillow:mainfrom
RavSinghChandan:imageops-palette-lut
Oct 16, 2026
Merged

radarhere merged 5 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
@radarhere radarhere changed the title Apply invert, posterize and solarize to the palette of a P image Apply support for ImageOps invert, posterize and solarize of P images Oct 16, 2026
@radarhere
radarhere force-pushed the imageops-palette-lut branch from 6c4baeb to 9751e0f Compare October 16, 2026 00:08
@radarhere radarhere changed the title Apply support for ImageOps invert, posterize and solarize of P images Add support for ImageOps invert, posterize and solarize of P images Oct 16, 2026
@radarhere
radarhere enabled auto-merge (squash) October 16, 2026 00:08
@radarhere
radarhere merged commit fef1035 into python-pillow:main Oct 16, 2026
51 checks passed
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