Skip to content

Speed up Image.getchannel(), Image.merge(), Image.putalpha() and Image.split()#9675

Open
akx wants to merge 1 commit into
python-pillow:mainfrom
akx:bands-loops
Open

Speed up Image.getchannel(), Image.merge(), Image.putalpha() and Image.split()#9675
akx wants to merge 1 commit into
python-pillow:mainfrom
akx:bands-loops

Conversation

@akx

@akx akx commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Refs #9649 (same technique, different loops).

On my machine, this speeds up .merge() for RGB images by about 3x according to the benchmark code from #9654.

@radarhere radarhere changed the title Speed up .merge()/.split() and friends Speed up Image.getchannel(), Image.merge(), Image.putalpha() and Image.split() Jun 16, 2026
Comment thread src/libImaging/Bands.c
@@ -47,16 +47,19 @@ ImagingGetBand(Imaging imIn, int band) {
}

/* Extract band from image */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Extract band from image */
// Extract band from image

Maybe this is nicer than having a multiline comment next to a single line comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it matters that much? I just didn't want to touch the original code or comment format here :)

Comment thread src/libImaging/Bands.c
@@ -92,33 +95,37 @@ ImagingSplit(Imaging imIn, Imaging bands[4]) {
}

/* Extract bands from image */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Extract bands from image */
// Extract bands from image

Comment thread src/libImaging/Bands.c
@@ -195,10 +202,13 @@ ImagingPutBand(Imaging imOut, Imaging imIn, int band) {
}

/* Insert band into image */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Insert band into image */
// Insert band into image

Comment thread src/libImaging/Bands.c Outdated
@mergify

This comment was marked as outdated.

@hugovk

hugovk commented Jun 29, 2026

Copy link
Copy Markdown
Member

Updated now #9654 is merged.

@codspeed-hq

codspeed-hq Bot commented Jun 29, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 85.18%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 3 improved benchmarks
✅ 328 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_merge[1024x1024-RGBA] 10.1 ms 5.4 ms +87.77%
test_merge[1024x1024-RGB] 8.4 ms 4.5 ms +86.05%
test_merge[1024x1024-LA] 6.7 ms 3.7 ms +81.78%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing akx:bands-loops (23f5f76) with main (086fa4a)

Open in CodSpeed

Comment thread src/libImaging/Bands.c Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants