diff --git a/Tests/test_image.py b/Tests/test_image.py index ca522cf3803..3f28d4e3851 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -514,21 +514,31 @@ def test_no_supported_modes_method(self, tmp_path: Path) -> None: im = hopper() im.save(temp_file, convert_mode=True) - def test_convert_mode(self) -> None: - for mode, modes in [["P", []], ["P", ["P"]]]: # no modes, same mode - im = Image.new(mode, (100, 100)) - assert im._convert_mode(modes) is None - - for mode, modes in [ - ["P", ["RGB"]], - ["P", ["L"]], # converting to a non-preferred mode - ["LA", ["P"]], - ["I", ["L"]], - ["RGB", ["L"]], - ["RGB", ["CMYK"]], - ]: - im = Image.new(mode, (100, 100)) - assert im._convert_mode(modes) is not None + @pytest.mark.parametrize( + "mode, modes", + ( + ("P", ["RGB"]), + ("P", ["L"]), # converting to a non-preferred mode + ("LA", ["P"]), + ("I", ["L"]), + ("RGB", ["L"]), + ("RGB", ["CMYK"]), + ), + ) + def test_convert_mode(self, mode: str, modes: list[str]) -> None: + im = Image.new(mode, (100, 100)) + assert im._convert_mode(modes) is not None + + @pytest.mark.parametrize( + "mode, modes", + ( + ("P", []), # no mode + ("P", ["P"]), # same mode + ), + ) + def test_convert_mode_noop(self, mode: str, modes: list[str]) -> None: + im = Image.new(mode, (100, 100)) + assert im._convert_mode(modes) is None def test_effect_mandelbrot(self) -> None: # Arrange diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 09f1fb7f4e8..74d48ea0e24 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -1181,7 +1181,7 @@ def write(self, data: Buffer) -> int: return fp.data -def _supported_modes(): +def _supported_modes() -> list[str]: return ["RGB", "RGBA", "P", "I", "F", "LA", "L", "1"] diff --git a/src/PIL/Image.py b/src/PIL/Image.py index a5ada6c50c8..d2eff91d740 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2618,9 +2618,11 @@ def save( if open_fp: fp.close() - def _convert_mode(self, modes, params={}): + def _convert_mode( + self, modes: list[str], params: dict[str, Any] = {} + ) -> Image | None: if not modes or self.mode in modes: - return + return None if self.mode == "P": preferred_modes = [] if "A" in self.im.getpalettemode(): @@ -2674,6 +2676,8 @@ def _convert_mode(self, modes, params={}): elif new_mode: return self.convert(new_mode) + return None + def seek(self, frame: int) -> None: """ Seeks to the given frame in this sequence file. If you seek diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 97fc5747f4f..cc02b29c9bd 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -891,7 +891,7 @@ def jpeg_factory( return im -def _supported_modes(): +def _supported_modes() -> list[str]: return ["RGB", "CMYK", "YCbCr", "RGBX", "L", "1"] diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index 244db454569..543a7c1708f 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -1532,7 +1532,7 @@ def append(fp: IO[bytes], cid: bytes, *data: bytes) -> None: return chunks -def _supported_modes(): +def _supported_modes() -> list[str]: return ["RGB", "RGBA", "P", "I", "LA", "L", "1"] diff --git a/src/PIL/WebPImagePlugin.py b/src/PIL/WebPImagePlugin.py index 2d951d23e33..ee7c9e74787 100644 --- a/src/PIL/WebPImagePlugin.py +++ b/src/PIL/WebPImagePlugin.py @@ -314,7 +314,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: fp.write(data) -def _supported_modes(): +def _supported_modes() -> list[str]: return [ "RGB", "RGBA",