Skip to content

Remove unused ImagingConvertInPlace - #9878

Merged
hugovk merged 3 commits into
python-pillow:mainfrom
radarhere:convert
Aug 28, 2026
Merged

Remove unused ImagingConvertInPlace#9878
hugovk merged 3 commits into
python-pillow:mainfrom
radarhere:convert

Conversation

@radarhere

@radarhere radarhere commented Aug 19, 2026

Copy link
Copy Markdown
Member

im.putalpha() is the only place where C's setmode() is called.

Pillow/src/PIL/Image.py

Lines 2050 to 2052 in 3220aca

mode = getmodebase(self.mode) + "A"
try:
self.im.setmode(mode)

This is only trying to convert the image to LA, PA or RGBA.

However, the only operation setmode() can actually succeed at is converting RGB or RGBX to RGBA.

Pillow/src/_imaging.c

Lines 2079 to 2090 in 3220aca

if (im->mode == mode) {
; /* same mode; always succeeds */
} else if (isRGB(im->mode) && isRGB(mode)) {
/* color to color */
im->mode = mode;
im->bands = modelen;
if (mode == IMAGING_MODE_RGBA) {
(void)ImagingFillBand(im, 3, 255);
}
} else {
/* trying doing an in-place conversion */
if (!ImagingConvertInPlace(im, mode)) {

ImagingConvertInPlace(Imaging imIn, const ModeID mode) {
ImagingSectionCookie cookie;
ImagingShuffler convert;
int y;
/* limited support for inplace conversion */
if (imIn->mode == IMAGING_MODE_L && mode == IMAGING_MODE_1) {
convert = l2bit;
} else if (imIn->mode == IMAGING_MODE_1 && mode == IMAGING_MODE_L) {
convert = bit2l;

ImagingConvertInPlace() isn't able to convert anything to LA, PA or RGBA.

So this PR

  • removes ImagingConvertInPlace(). setmode() is the only place where ImagingConvertInPlace() is called.
  • simplifies setmode() to more clearly only convert RGB or RGBX images to RGBA. I've also renamed it to setalpha().

@akx akx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I had been looking at removing ImagingConvertInPlace (in a bid to see if the shuffler functions could be implemented in a faster way) but didn't dare go through with it.

Some thoughts within though :)

Comment thread src/_imaging.c
static PyObject *
im_setmode(ImagingObject *self, PyObject *args) {
im_setalpha(ImagingObject *self, PyObject *args) {
/* attempt to modify the mode of an image in place */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This comment is a little stale now?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Is it? The method is still modifying the mode of an image in place. It's more specific now, is all.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I suppose...

Should this function know how to do P-to-PA and L-to-LA too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The thought occurred to me. It is a bit different to RGB, since RGB already has the same pixelsize as RGBA.

Do you mind if that is a follow-up PR? I would rather tidy things up first.

Comment thread src/_imaging.c
Comment thread src/_imaging.c Outdated
@hugovk

hugovk commented Aug 28, 2026

Copy link
Copy Markdown
Member

I'll click the rebase button to test automerge (#9126) on a non-docs PR:

image

@hugovk
hugovk enabled auto-merge August 28, 2026 08:02
@hugovk
hugovk merged commit 20510d4 into python-pillow:main Aug 28, 2026
72 of 79 checks passed
@radarhere
radarhere deleted the convert branch August 28, 2026 08:44
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