From 96eef130bfdb455ee2e0a592bcd5310bcd7a383a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 7 Oct 2022 09:46:01 +1100 Subject: [PATCH] Do not attempt normalization if image is already normal --- Tests/test_image_convert.py | 5 +++++ src/PIL/Image.py | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py index 1a78f8b4c4c..0cd017a4b35 100644 --- a/Tests/test_image_convert.py +++ b/Tests/test_image_convert.py @@ -38,6 +38,11 @@ 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