diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 1d6469819..1ffd012aa 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -135,6 +135,10 @@ def test_trns_l(tmp_path): f = str(tmp_path / "temp.png") + im_la = im.convert("LA") + assert "transparency" not in im_la.info + im_la.save(f) + im_rgb = im.convert("RGB") assert im_rgb.info["transparency"] == (128, 128, 128) # undone im_rgb.save(f) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index c9265b5ab..352d77fe7 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -975,7 +975,9 @@ class Image: delete_trns = False # transparency handling if has_transparency: - if self.mode in ("1", "L", "I", "RGB") and mode == "RGBA": + if (self.mode in ("1", "L", "I") and mode in ("LA", "RGBA")) or ( + self.mode == "RGB" and mode == "RGBA" + ): # Use transparent conversion to promote from transparent # color to an alpha channel. new_im = self._new( diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 0f200af6b..5a632ca42 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -1634,29 +1634,15 @@ ImagingConvertTransparent(Imaging imIn, const char *mode, int r, int g, int b) { return (Imaging)ImagingError_ModeError(); } - if (!((strcmp(imIn->mode, "RGB") == 0 || strcmp(imIn->mode, "1") == 0 || - strcmp(imIn->mode, "I") == 0 || strcmp(imIn->mode, "L") == 0) && - strcmp(mode, "RGBA") == 0)) -#ifdef notdef - { - return (Imaging)ImagingError_ValueError("conversion not supported"); - } -#else - { - static char buf[100]; - snprintf( - buf, - 100, - "conversion from %.10s to %.10s not supported in convert_transparent", - imIn->mode, - mode); - return (Imaging)ImagingError_ValueError(buf); - } -#endif - - if (strcmp(imIn->mode, "RGB") == 0) { + if (strcmp(imIn->mode, "RGB") == 0 && strcmp(mode, "RGBA") == 0) { convert = rgb2rgba; - } else { + } else if ((strcmp(imIn->mode, "1") == 0 || + strcmp(imIn->mode, "I") == 0 || + strcmp(imIn->mode, "L") == 0 + ) && ( + strcmp(mode, "RGBA") == 0 || + strcmp(mode, "LA") == 0 + )) { if (strcmp(imIn->mode, "1") == 0) { convert = bit2rgb; } else if (strcmp(imIn->mode, "I") == 0) { @@ -1665,6 +1651,15 @@ ImagingConvertTransparent(Imaging imIn, const char *mode, int r, int g, int b) { convert = l2rgb; } g = b = r; + } else { + static char buf[100]; + snprintf( + buf, + 100, + "conversion from %.10s to %.10s not supported in convert_transparent", + imIn->mode, + mode); + return (Imaging)ImagingError_ValueError(buf); } imOut = ImagingNew2Dirty(mode, imOut, imIn);