Skip to content

Speed up and simplify GIF optimization - #10074

Merged
radarhere merged 2 commits into
python-pillow:mainfrom
akx:gif-imagemath
Sep 28, 2026
Merged

radarhere merged 2 commits into
python-pillow:mainfrom
akx:gif-imagemath

Conversation

@akx

@akx akx commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

While working on #10072, I was looking for uses of ImageMath around Pillow, and this seemed like a pretty complex construction and there indeed was a simpler one lurking in there... 😄

ImageChops was optimized in 4b5a6e3 and Image.point would be optimized by #9743. #9829 would improve (though currently slightly slow down 😞) quantization accuracy.

There are no new tests or benchmarks committed; the test added in #7568 covers this.

This also requires less intermediate images, so peak memory use decreases too (30% to 80% on my machine, depending on whether we're talking shared palette or a per-frame one).

I benchmarked this locally:

-------------------------- benchmark 'gif': 4 tests, 2 sources ---------------------------
Name (time in ms)                             0001_5600015 Min  0002_b491b6e Min      ΔMin
------------------------------------------------------------------------------------------
test_save_gif[shared-palette-plain]                    13.4083           12.9534     -3.4%
test_save_gif[shared-palette-optimize]                 82.2019           13.9369    -83.0%
test_save_gif[per-frame-palette-plain]                443.2780          429.3091     -3.2%
test_save_gif[per-frame-palette-optimize]             462.2850          439.6624     -4.9%
------------------------------------------------------------------------------------------

The per-frame-palette case doesn't get optimized much since it's dominated by palette quantization, not this.

@radarhere radarhere added the GIF label Sep 28, 2026
@akx
akx marked this pull request as ready for review September 28, 2026 10:22
Comment thread src/PIL/GifImagePlugin.py Outdated
@radarhere
radarhere merged commit 38c17ed into python-pillow:main Sep 28, 2026
57 of 59 checks passed
@akx
akx deleted the gif-imagemath branch September 28, 2026 13:53
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.

2 participants