From 39e1c7fe43b9eeeedf793a9db0e11e28ccd027ed Mon Sep 17 00:00:00 2001 From: YousefStudio Date: Mon, 23 Nov 2026 14:23:38 +0300 Subject: [PATCH 1/3] Fix autocontrast cutoff tuple length validation --- src/PIL/ImageOps.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index e76d3717c41..bd0d815ec72 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -121,6 +121,10 @@ def autocontrast( :return: An image. """ + if isinstance(cutoff, tuple) and len(cutoff) != 2: + msg = "cutoff must be a tuple of two values" + raise ValueError(msg) + if ignore is not None: if isinstance(ignore, int): ignore = (ignore,) From 227381af1afd27ca6699ae3df9ad85c43adc27bc Mon Sep 17 00:00:00 2001 From: YousefStudio Date: Mon, 23 Nov 2026 14:23:41 +0300 Subject: [PATCH 2/3] Test invalid autocontrast cutoff tuple lengths --- Tests/test_imageops.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 00b18fd3c58..7905204eda2 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -635,6 +635,13 @@ def autocontrast(cutoff: int | tuple[int, int]) -> list[int]: assert autocontrast(10) != autocontrast((1, 10)) +@pytest.mark.parametrize("cutoff", ((), (10,), (10, 10, 10))) +def test_autocontrast_invalid_cutoff(cutoff: tuple[int, ...]) -> None: + im = Image.new("L", (1, 1)) + with pytest.raises(ValueError, match="cutoff must be a tuple of two values"): + ImageOps.autocontrast(im, cutoff=cutoff) + + @pytest.mark.parametrize("ignore", (-1, 256, [0, -1], [0, 256])) def test_autocontrast_invalid_ignore(ignore: int | list[int]) -> None: im = Image.new("L", (1, 1)) From 0560c80a499e7f789e1056cf4508836f0f681d55 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 23 Nov 2026 23:17:49 +1100 Subject: [PATCH 3/3] Lint fix --- Tests/test_imageops.py | 4 ++-- src/PIL/ImageOps.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 7905204eda2..bd733006606 100644 --- a/Tests/test_imageops.py +++ b/Tests/test_imageops.py @@ -638,8 +638,8 @@ def autocontrast(cutoff: int | tuple[int, int]) -> list[int]: @pytest.mark.parametrize("cutoff", ((), (10,), (10, 10, 10))) def test_autocontrast_invalid_cutoff(cutoff: tuple[int, ...]) -> None: im = Image.new("L", (1, 1)) - with pytest.raises(ValueError, match="cutoff must be a tuple of two values"): - ImageOps.autocontrast(im, cutoff=cutoff) + with pytest.raises(ValueError, match="cutoff must be either a tuple"): + ImageOps.autocontrast(im, cutoff=cutoff) # type: ignore[arg-type] @pytest.mark.parametrize("ignore", (-1, 256, [0, -1], [0, 256])) diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index bd0d815ec72..c0c9bb147fd 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -122,7 +122,9 @@ def autocontrast( :return: An image. """ if isinstance(cutoff, tuple) and len(cutoff) != 2: - msg = "cutoff must be a tuple of two values" + msg = ( # type: ignore[unreachable] + "cutoff must be either a tuple of (low, high), or a single number for both" + ) raise ValueError(msg) if ignore is not None: