Skip to content

Make .quantize(palette=...) use exact colors when possible, and improve dithering quality - #9829

Open
akx wants to merge 11 commits into
python-pillow:mainfrom
akx:palettize-fixes
Open

akx wants to merge 11 commits into
python-pillow:mainfrom
akx:palettize-fixes

Conversation

@akx

@akx akx commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Refs #1852 (fixes the reproducer bug: we now select exact colors when available, but we're still not necessarily selecting the mathematically nearest color when quantizing to a given palette).

  • Refactors the topalette() conversion function to a separate file for ease of understanding
  • Refactors the topalette() function to multiple functions for less nesting and ease of reading
  • Improves internal documentation/constants around palettes
  • Fixes a couple possible but implausible NULL pointer derefs (that could occur if there was less than ~1100 bytes of memory available for Python)
  • The usual™️ hoist + restrict optimizations for internals
  • Adds benchmarks for quantize-into-palette

... and then the main thing:

  • Adds a small linear-probing hash table for finding exact matches in the palette before looking a close-enough one up in the cache "cube" in the palette. In other words, if a palette specifies a color, we will use it, guaranteed.
  • 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: this now also includes the changes from #9338 as the last commit, since that touches the same files as this jiggles around.

Closes #9338 (includes it).

@akx
akx force-pushed the palettize-fixes branch 4 times, most recently from 7708c8a to 3eb5509 Compare July 29, 2026 08:42
@codspeed

codspeed Bot commented Jul 29, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 38.19%

3 warnings

⚠️ 2 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 24 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 24 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

❌ 24 regressed benchmarks
✅ 644 untouched benchmarks
⏩ 352 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_quantize_to_palette[1237x811-exact-flower2.jpg-P-floyd-steinberg] 41.8 ms 85.6 ms -51.08%
❌ test_quantize_to_palette[1237x811-exact-synthetic-P-floyd-steinberg] 41.6 ms 84.9 ms -51.04%
❌ test_quantize_to_palette[1237x811-exact-flower2.jpg-P-none] 20.5 ms 39 ms -47.59%
❌ test_quantize_to_palette[1237x811-exact-synthetic-P-none] 20.3 ms 38.4 ms -47.25%
❌ test_quantize_to_palette[1237x811-exact-flower2.jpg-PA-floyd-steinberg] 49.4 ms 92.4 ms -46.5%
❌ test_quantize_to_palette[1237x811-exact-synthetic-PA-floyd-steinberg] 49.2 ms 91.7 ms -46.33%
❌ test_quantize_to_palette[1237x811-grayscale-flower2.jpg-P-floyd-steinberg] 42.8 ms 78.1 ms -45.17%
❌ test_quantize_to_palette[1237x811-grayscale-synthetic-P-floyd-steinberg] 45.5 ms 80.7 ms -43.65%
❌ test_quantize_to_palette[1237x811-web-synthetic-P-floyd-steinberg] 47.4 ms 82.7 ms -42.62%
❌ test_quantize_to_palette[1237x811-web-flower2.jpg-P-floyd-steinberg] 48 ms 83.3 ms -42.31%
❌ test_quantize_to_palette[1237x811-grayscale-flower2.jpg-PA-floyd-steinberg] 50.5 ms 85 ms -40.61%
❌ test_quantize_to_palette[1237x811-grayscale-synthetic-PA-floyd-steinberg] 52.9 ms 87.3 ms -39.47%
❌ test_quantize_to_palette[1237x811-web-synthetic-PA-floyd-steinberg] 54.9 ms 89.3 ms -38.55%
❌ test_quantize_to_palette[1237x811-web-flower2.jpg-PA-floyd-steinberg] 55.5 ms 90 ms -38.31%
❌ test_quantize_to_palette[1237x811-exact-flower2.jpg-PA-none] 28.4 ms 45 ms -36.79%
❌ test_quantize_to_palette[1237x811-exact-synthetic-PA-none] 28.2 ms 44.3 ms -36.35%
❌ test_quantize_to_palette[1237x811-web-flower2.jpg-P-none] 22.9 ms 33.3 ms -31.09%
❌ test_quantize_to_palette[1237x811-grayscale-flower2.jpg-P-none] 22.9 ms 33.2 ms -31.08%
❌ test_quantize_to_palette[1237x811-web-synthetic-P-none] 23.8 ms 34.2 ms -30.27%
❌ test_quantize_to_palette[1237x811-grayscale-synthetic-P-none] 23.8 ms 34.1 ms -30.25%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing akx:palettize-fixes (fca6364) with main (c5dd59a)

Open in CodSpeed

Footnotes

  1. 352 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@akx
akx force-pushed the palettize-fixes branch 2 times, most recently from 3fd1f54 to 7fa4a06 Compare August 11, 2026 10:51
@akx
akx marked this pull request as ready for review August 11, 2026 12:53
@akx

akx commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

I think we'll have to accept some performance degradation in exchange for actually making conversions accurate...

Comment thread src/libImaging/ConvertToPalette.c Resolved
Comment thread src/libImaging/ConvertToPalette.c Resolved
@akx akx changed the title Make .quantize(palette=...) use exact colors when possible Make .quantize(palette=...) use exact colors when possible, and improve dithering quality Sep 8, 2026
@akx
akx force-pushed the palettize-fixes branch from 22dfe86 to 17e8a00 Compare October 18, 2026 00:26
@akx

akx commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

Rebased.

@akx

akx commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased..

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants