From c4479adff58e48fd68912b522047fe126509b494 Mon Sep 17 00:00:00 2001 From: tony1233200 Date: Mon, 16 Nov 2026 15:41:13 +0000 Subject: [PATCH 1/2] Convert LA images when pasting into RGB images --- Tests/test_image_paste.py | 16 ++++++++++++++++ docs/releasenotes/13.1.0.rst | 9 +++++++++ src/PIL/Image.py | 2 +- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Tests/test_image_paste.py b/Tests/test_image_paste.py index 2cd44feaab0..22d93c3d91a 100644 --- a/Tests/test_image_paste.py +++ b/Tests/test_image_paste.py @@ -127,6 +127,22 @@ def gradient_RGBa(self) -> Image.Image: ], ) + def test_paste_la_into_rgb(self) -> None: + # LA images are stored with two padding bytes per pixel. + # Unlike Image.new(), Image.merge() and point() leave them as zero, + # so the image cannot be pasted into an RGB image without conversion + merged = Image.merge( + "LA", (Image.new("L", (1, 1), 128), Image.new("L", (1, 1), 255)) + ) + for source in ( + Image.new("LA", (1, 1), (128, 255)), + merged, + Image.new("LA", (1, 1), (128, 255)).point(lambda v: v), + ): + im = Image.new("RGB", (1, 1)) + im.paste(source, (0, 0)) + assert im.getpixel((0, 0)) == (128, 128, 128) + @pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"]) def test_image_solid(self, mode: str) -> None: im = Image.new(mode, (200, 200), "red") diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index 44729c75bad..1d1f3e9dcb7 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -67,6 +67,15 @@ Two new image processing operations are available in ImageOps. Other changes ============= +Pasting LA images into RGB images +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +:py:meth:`~PIL.Image.Image.paste` now converts LA images when pasting them into +RGB images, instead of copying the stored bytes directly. LA images created by +:py:meth:`~PIL.Image.merge` or :py:meth:`~PIL.Image.Image.point` have zeroed +padding bytes, and pasting them previously used those bytes as the green and +blue channels, producing red pixels. + Rounding crop coordinates ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 1b34480217c..be8e692f828 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1957,7 +1957,7 @@ def paste( source = ImageColor.getcolor(im, self.mode) elif isinstance(im, Image): if self.mode != im.mode and ( - self.mode != "RGB" or im.mode not in {"LA", "RGBA", "RGBa"} + self.mode != "RGB" or im.mode not in {"RGBA", "RGBa"} ): # should use an adapter for this! im = im.convert(self.mode) From 50777de262d260a97eab4227e22906201d68633a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 17 Nov 2026 06:42:12 +1100 Subject: [PATCH 2/2] Simplify test --- Tests/test_image_paste.py | 27 +++++++++++---------------- docs/releasenotes/13.1.0.rst | 9 --------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/Tests/test_image_paste.py b/Tests/test_image_paste.py index 22d93c3d91a..e8f65ad8f6b 100644 --- a/Tests/test_image_paste.py +++ b/Tests/test_image_paste.py @@ -127,22 +127,6 @@ def gradient_RGBa(self) -> Image.Image: ], ) - def test_paste_la_into_rgb(self) -> None: - # LA images are stored with two padding bytes per pixel. - # Unlike Image.new(), Image.merge() and point() leave them as zero, - # so the image cannot be pasted into an RGB image without conversion - merged = Image.merge( - "LA", (Image.new("L", (1, 1), 128), Image.new("L", (1, 1), 255)) - ) - for source in ( - Image.new("LA", (1, 1), (128, 255)), - merged, - Image.new("LA", (1, 1), (128, 255)).point(lambda v: v), - ): - im = Image.new("RGB", (1, 1)) - im.paste(source, (0, 0)) - assert im.getpixel((0, 0)) == (128, 128, 128) - @pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"]) def test_image_solid(self, mode: str) -> None: im = Image.new(mode, (200, 200), "red") @@ -153,6 +137,17 @@ def test_image_solid(self, mode: str) -> None: im = im.crop((12, 23, im2.width + 12, im2.height + 23)) assert_image_equal(im, im2) + def test_image_la_into_rgb(self) -> None: + # LA images are stored with two padding bytes per pixel. + # point() sets them to zero, + # so the image must be internally converted before pasting onto RGB + im = Image.new("RGB", (1, 1)) + + source = Image.new("LA", (1, 1), (128, 255)).point(lambda v: v) + im.paste(source) + + assert im.getpixel((0, 0)) == (128, 128, 128) + @pytest.mark.parametrize("y", [10, -10]) @pytest.mark.parametrize("mode", ["L", "RGB"]) @pytest.mark.parametrize("mask_mode", ["", "1", "L", "LA", "RGBa"]) diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index 1d1f3e9dcb7..44729c75bad 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -67,15 +67,6 @@ Two new image processing operations are available in ImageOps. Other changes ============= -Pasting LA images into RGB images -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -:py:meth:`~PIL.Image.Image.paste` now converts LA images when pasting them into -RGB images, instead of copying the stored bytes directly. LA images created by -:py:meth:`~PIL.Image.merge` or :py:meth:`~PIL.Image.Image.point` have zeroed -padding bytes, and pasting them previously used those bytes as the green and -blue channels, producing red pixels. - Rounding crop coordinates ^^^^^^^^^^^^^^^^^^^^^^^^^