From b8cd3e72a6ffac5af0dcc4df3fc3eabf4bb16756 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 7 Oct 2022 09:48:56 +1100 Subject: [PATCH] Do not attempt normalization if image is already normal --- Tests/test_image_convert.py | 6 ++++++ src/PIL/Image.py | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 1a78f8b4c4c..e573a06a2b2 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -38,6 +38,12 @@ def convert(im, mode): convert(im, output_mode) +def test_unsupported_conversion(): + im = hopper() + with pytest.raises(ValueError): + im.convert("INVALID") + + def test_default(): im = hopper("P") diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 6611ceb3c7e..cbf3ff86535 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1036,7 +1036,10 @@ def convert_transparency(m, v): except ValueError: try: # normalize source image and try again - im = self.im.convert(getmodebase(self.mode)) + modebase = getmodebase(self.mode) + if modebase == self.mode: + raise + im = self.im.convert(modebase) im = im.convert(mode, dither) except KeyError as e: raise ValueError("illegal conversion") from e