From f00cdb4ceff1f79d0490d1cf8eda1b3525d91add Mon Sep 17 00:00:00 2001 From: Abhishek Prajapati Date: Thu, 3 Dec 2026 21:11:58 +0530 Subject: [PATCH] Preserve color-key transparency when inverting images --- Tests/test_imageops.py | 25 +++++++++++++++++++++++++ docs/releasenotes/13.1.0.rst | 6 ++++++ src/PIL/ImageOps.py | 12 +++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 57151bc9670..22e784ff46d 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -775,6 +775,31 @@ def test_palette_operation_keeps_alpha() -> None: assert palette == [255, 255, 255, 0, 0, 255, 255, 128] +@pytest.mark.parametrize( + "mode, transparent, opaque", + ( + ("1", 0, 255), + ("1", 255, 0), + ("L", 31, 224), + ("RGB", (1, 12, 32), (254, 243, 223)), + ), +) +def test_invert_keeps_transparent_pixels( + mode: str, transparent: int | tuple[int, ...], opaque: int | tuple[int, ...] +) -> None: + im = Image.new(mode, (2, 1), transparent) + im.putpixel((1, 0), opaque) + im.info["transparency"] = transparent + + result = ImageOps.invert(im) + + assert result.mode == mode + assert result.getpixel((0, 0)) == opaque + assert result.info["transparency"] == opaque + assert im.info["transparency"] == transparent + assert result.convert("RGBA").getchannel("A").tobytes() == b"\x00\xff" + + def test_palette_histogram_operation_not_supported() -> None: im = hopper("P") with pytest.raises(NotImplementedError, match="mode P support coming soon"): diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index ad1ff23a499..54ec9a4c40d 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -93,6 +93,12 @@ Two new image processing operations are available in ImageOps. Other changes ============= +Preserve transparency when inverting images +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +:py:func:`~PIL.ImageOps.invert` now updates the transparent color in ``1``, ``L`` +and ``RGB`` images so that the same pixels remain transparent after inversion. + PCX and ZIP decoder row sizes ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index d3483b58bac..b55c128dfad 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -786,7 +786,17 @@ def invert(image: Image.Image) -> Image.Image: :return: An image. """ lut = list(range(255, -1, -1)) - return image.point(lut) if image.mode == "1" else _lut(image, lut, palette=True) + result = image.point(lut) if image.mode == "1" else _lut(image, lut, palette=True) + + transparency = result.info.get("transparency") + # Match the 8-bit samples used when converting the color key to alpha. + if image.mode in ("1", "L") and isinstance(transparency, int): + result.info["transparency"] = lut[transparency & 255] + elif image.mode == "RGB" and isinstance(transparency, (tuple, list)): + result.info["transparency"] = tuple( + lut[channel & 255] for channel in transparency + ) + return result def mirror(image: Image.Image) -> Image.Image: