diff --git a/Tests/test_imageops.py b/Tests/test_imageops.py index 00b18fd3c58..bd733006606 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 either a tuple"): + ImageOps.autocontrast(im, cutoff=cutoff) # type: ignore[arg-type] + + @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)) diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index e76d3717c41..c0c9bb147fd 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -121,6 +121,12 @@ def autocontrast( :return: An image. """ + if isinstance(cutoff, tuple) and len(cutoff) != 2: + 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: if isinstance(ignore, int): ignore = (ignore,)