Speed up Image.blend()#9649
Open
akx wants to merge 3 commits into
Open
Conversation
radarhere
reviewed
Jun 2, 2026
Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com>
Member
|
Please can you share |
Contributor
Author
|
@hugovk Sure thing: import pytest
from PIL import Image
@pytest.fixture(scope="module")
def images() -> tuple[Image.Image, Image.Image]:
size = (2048, 2048)
im1 = Image.new("RGB", size)
im2 = Image.new("RGB", size)
im1.frombytes(bytes(i % 256 for i in range(im1.width * im1.height * 3)))
im2.frombytes(bytes((i * 7 + 31) % 256 for i in range(im2.width * im2.height * 3)))
return im1, im2
def test_blend_perf(benchmark, images: tuple[Image.Image, Image.Image]) -> None:
im1, im2 = images
result = benchmark(Image.blend, im1, im2, 0.5)
assert result.size == im1.size |
Contributor
Author
|
Btw, if this is merged, I can take a look at other similar loops and apply the same optimizations - looks like there could be lots of easy perf pickings 😊 |
radarhere
approved these changes
Jun 2, 2026
This was referenced Jun 2, 2026
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.
I was working on a project that uses
Image.blend()and it showed up pretty red in a Viztracer timeline, so I decided to investigate a little.Very small patch, but allows the C compiler to optimize the loops better:
ysizeandlinesizecan't change during iteration (but the compiler can't statically know that), so hoisting those to localsimOutis a fresh allocation and can't alias another pointer, so addingrestrictto note that.restrictshould be available on MSVC in C99 mode without having to use the MS extension name__restrict; we'll see how it pans out in CI and adjust accordingly.In addition, added a little fast-path if you pass in the same image object twice.
On my machine, this shows a 300% speed improvement (with
blend_bench.pybeing a pytest-benchmark suite doingbenchmark(Image.blend, im1, im2, 0.5)on two 2048x2048 RGB images).I also looked at using the integer math from
AlphaComposite.c, but that only made things a little slower.