Skip to content

Support 16-bit images in Image.blend - #10551

Merged
radarhere merged 4 commits into
python-pillow:mainfrom
radarhere:blend
Nov 18, 2026
Merged

radarhere merged 4 commits into
python-pillow:mainfrom
radarhere:blend

Conversation

@radarhere

@radarhere radarhere commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Separating the blend part of #10549. Helps #7397

Changes:

  1. src/libImaging/Blend.c:
    • Expanded ImagingBlend to accept IMAGING_TYPE_I16 in addition to IMAGING_TYPE_UINT8.
    • Handled endianness via isImageI16BigEndian().
    • Added interpolation and extrapolation loops with $[0, 65535]$ clamping.
  2. Tests:
    • Added unit test in Tests/test_image.py for Image.blend with 16-bit images.

I've also applied roundf to Blend.c generally.

@codspeed

codspeed Bot commented Nov 18, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 87.61%

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

⚠️ 4 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

⚠️ 4 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

❌ 4 regressed benchmarks
✅ 664 untouched benchmarks
⏩ 352 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_blend[1237x811-RGB] 12.9 ms 105.6 ms -87.79%
❌ test_blend[1237x811-LA] 12.9 ms 104.5 ms -87.68%
❌ test_blend[1237x811-RGBA] 12.9 ms 104.6 ms -87.67%
❌ test_blend[1237x811-L] 3.4 ms 26.4 ms -87.28%

Tip

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


Comparing radarhere:blend (85e2cbf) with main (af77f16)

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. ↩

@radarhere
radarhere merged commit 417dad2 into python-pillow:main Nov 18, 2026
62 of 64 checks passed
@radarhere
radarhere deleted the blend branch November 18, 2026 12:21
@akx

akx commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

@radarhere Did you notice the performance numbers for this? 😕

EDIT: the culprit seems to be roundf; what's the reason for the "Use roundf" commit?

@radarhere

Copy link
Copy Markdown
Member Author

I don't have anything very inventive to say at this point. Because alpha is a float, and we're turning the calculation result into a UINT8 or UINT16? Is there any particular reason we should sacrifice precision for performance in this instance?

@akx

akx commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

I wasn't asking for inventive, just... reasons, and whether you'd noticed the numbers.

But applying that generally (and the test change that needed to come with it, I suppose) in a PR that is supposed to be about 16-bit support seemed odd. Sure, Pillow had been blending slightly wrong "forever", but now this behavior (accuracy) change for other modes is hidden in a seemingly unrelated PR.

I originally found out about this because I was plotting Pillow performance (the benchmarks from current main, run over all revisions) over the last year:

Speed index (lower better)

Screenshot 2026-11-27 at 10 59 00

The uptick at the end caught my interest. (It's still computing intermediate revisions, hence the varied resolution.)

Slowdowns

Screenshot 2026-11-27 at 10 55 14

Anyhow: please see https://github.com/python-pillow/Pillow/compare/main...akx:Pillow:blend-minimal?expand=1 for a small patch to restore some performance with the same precise results – I can't open a PR of it myself for the usual reasons.

And #10615 should fix test_draw_lines, of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants