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
UINT8 *in = imIn->image8[y];
UINT8 *out = (UINT8 *)imOut->image[y] + band;
for (x = 0; x < imIn->xsize; x++) {
// restrict safe: imIn and imOut are distinct images (sizes checked above).

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.

I don't understand what 'sizes checked above' has to do with this.

@mergify

mergify Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

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.

2 participants