Make .quantize(palette=...) use exact colors when possible - #9829
Draft
akx wants to merge 13 commits into
Draft
Conversation
The old image was (e.g. in the case of palette optimization) pessimal in some cases.
akx
force-pushed
the
palettize-fixes
branch
2 times, most recently
from
July 29, 2026 08:20
c0ecde9 to
7708c8a
Compare
This affects `.quantize(palette=...)`. Refs python-pillow#1852
They were used nowhere else. Keeping them together, as a private API, allows for future improvements to the palette cache's shape.
This was referenced Jul 29, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #9831 so we're measuring the right things...
Refs #1852 (fixes the reproducer bug, but we're still not necessarily selecting the mathematically nearest color when quantizing to a given palette).
topalette()conversion function to a separate file for ease of understandingtopalette()function to multiple functions for less nesting and ease of reading... and then the main thing:
ImagingPaletteCache*functions are made private, since the structure of the cache was undocumented and private anyway; we need to add a new flag in the cache entries to have a better guess of when we might have an exact match for a color.EDIT: Edited away the benchmark results, there's something off here still :)