From e79d1746f29412c069d30770d86616aa3dafb1d4 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 26 Mar 2024 19:57:17 +1100 Subject: [PATCH] Support conversion from RGB to La --- Tests/test_image_convert.py | 4 ++++ src/PIL/Image.py | 2 +- src/libImaging/Convert.c | 8 ++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 77ce4e7d5..2fb45854a 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -187,6 +187,10 @@ def test_trns_RGB(tmp_path: Path) -> None: assert "transparency" not in im_la.info im_la.save(f) + im_la = im.convert("La") + assert "transparency" not in im_la.info + assert im_la.getpixel((0, 0)) == (0, 0) + im_p = im.convert("P") assert "transparency" in im_p.info im_p.save(f) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 11a3459ed..5c2309ac3 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -979,7 +979,7 @@ class Image: # transparency handling if has_transparency: if (self.mode in ("1", "L", "I", "I;16") and mode in ("LA", "RGBA")) or ( - self.mode == "RGB" and mode in ("LA", "RGBa", "RGBA") + self.mode == "RGB" and mode in ("La", "LA", "RGBa", "RGBA") ): # Use transparent conversion to promote from transparent # color to an alpha channel. diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 4db9eb700..0b84aa1ba 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -501,7 +501,7 @@ rgba2rgb_(UINT8 *out, const UINT8 *in, int xsize) { /* * Conversion of RGB + single transparent color either to * RGBA or LA, where any pixel matching the color will have the alpha channel set to 0, or - * RGBa, where any pixel matching the color will have all channels set to 0 + * RGBa or La, where any pixel matching the color will have all channels set to 0 */ static void @@ -942,6 +942,7 @@ static struct { {"RGB", "1", rgb2bit}, {"RGB", "L", rgb2l}, {"RGB", "LA", rgb2la}, + {"RGB", "La", rgb2la}, {"RGB", "I", rgb2i}, {"RGB", "F", rgb2f}, {"RGB", "BGR;15", rgb2bgr15}, @@ -1698,9 +1699,12 @@ ImagingConvertTransparent(Imaging imIn, const char *mode, int r, int g, int b) { if (strcmp(mode, "RGBa") == 0) { premultiplied = 1; } - } else if (strcmp(imIn->mode, "RGB") == 0 && strcmp(mode, "LA") == 0) { + } else if (strcmp(imIn->mode, "RGB") == 0 && (strcmp(mode, "LA") == 0 || strcmp(mode, "La") == 0)) { convert = rgb2la; source_transparency = 1; + if (strcmp(mode, "La") == 0) { + premultiplied = 1; + } } else if ((strcmp(imIn->mode, "1") == 0 || strcmp(imIn->mode, "I") == 0 || strcmp(imIn->mode, "I;16") == 0 ||