From 066e349b8e20f6e0400683ff988ac72eea24c049 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 20 Jun 2026 19:58:05 +1000 Subject: [PATCH 1/3] Simplify code by calling apply() from apply_in_place() --- src/PIL/ImageCms.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/PIL/ImageCms.py b/src/PIL/ImageCms.py index 513e28acf33..7745a192e03 100644 --- a/src/PIL/ImageCms.py +++ b/src/PIL/ImageCms.py @@ -327,9 +327,7 @@ def apply_in_place(self, im: Image.Image) -> Image.Image: if im.mode != self.output_mode: msg = "mode mismatch" raise ValueError(msg) # wrong output mode - self.transform.apply(im.getim(), im.getim()) - im.info["icc_profile"] = self.output_profile.tobytes() - return im + return self.apply(im, im) def get_display_profile(handle: SupportsInt | None = None) -> ImageCmsProfile | None: From 7f6e9d696913bba9fd1a6cf0b53f4a313dc25329 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 20 Jun 2026 20:04:33 +1000 Subject: [PATCH 2/3] Raise error if output image mode does not match transform output mode --- Tests/test_imagecms.py | 2 ++ src/PIL/ImageCms.py | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Tests/test_imagecms.py b/Tests/test_imagecms.py index d73ab34282b..8fe4c4c324d 100644 --- a/Tests/test_imagecms.py +++ b/Tests/test_imagecms.py @@ -196,6 +196,8 @@ def test_exceptions() -> None: psRGB = ImageCms.createProfile("sRGB") pLab = ImageCms.createProfile("LAB") t = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB") + with pytest.raises(ValueError, match="mode mismatch"): + t.apply(hopper("LAB"), hopper("RGBA")) with pytest.raises(ValueError, match="mode mismatch"): t.apply_in_place(hopper("RGBA")) diff --git a/src/PIL/ImageCms.py b/src/PIL/ImageCms.py index 7745a192e03..e91784089e8 100644 --- a/src/PIL/ImageCms.py +++ b/src/PIL/ImageCms.py @@ -317,16 +317,17 @@ def point(self, im: Image.Image) -> Image.Image: return self.apply(im) def apply(self, im: Image.Image, imOut: Image.Image | None = None) -> Image.Image: - if imOut is None: + if imOut is not None: + if imOut.mode != self.output_mode: + msg = "mode mismatch" + raise ValueError(msg) + else: imOut = Image.new(self.output_mode, im.size, None) self.transform.apply(im.getim(), imOut.getim()) imOut.info["icc_profile"] = self.output_profile.tobytes() return imOut def apply_in_place(self, im: Image.Image) -> Image.Image: - if im.mode != self.output_mode: - msg = "mode mismatch" - raise ValueError(msg) # wrong output mode return self.apply(im, im) From 642d806703c62d4160f3eb8ff25fb63cbedadd98 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 20 Jun 2026 21:18:14 +1000 Subject: [PATCH 3/3] Raise error if input image mode does not match transform input mode --- Tests/test_imagecms.py | 2 ++ src/PIL/ImageCms.py | 3 +++ 2 files changed, 5 insertions(+) diff --git a/Tests/test_imagecms.py b/Tests/test_imagecms.py index 8fe4c4c324d..3382a34e47c 100644 --- a/Tests/test_imagecms.py +++ b/Tests/test_imagecms.py @@ -196,6 +196,8 @@ def test_exceptions() -> None: psRGB = ImageCms.createProfile("sRGB") pLab = ImageCms.createProfile("LAB") t = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB") + with pytest.raises(ValueError, match="mode mismatch"): + t.apply(hopper("RGBA")) with pytest.raises(ValueError, match="mode mismatch"): t.apply(hopper("LAB"), hopper("RGBA")) with pytest.raises(ValueError, match="mode mismatch"): diff --git a/src/PIL/ImageCms.py b/src/PIL/ImageCms.py index e91784089e8..388a9296f8b 100644 --- a/src/PIL/ImageCms.py +++ b/src/PIL/ImageCms.py @@ -317,6 +317,9 @@ def point(self, im: Image.Image) -> Image.Image: return self.apply(im) def apply(self, im: Image.Image, imOut: Image.Image | None = None) -> Image.Image: + if im.mode != self.input_mode: + msg = "mode mismatch" + raise ValueError(msg) if imOut is not None: if imOut.mode != self.output_mode: msg = "mode mismatch"