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 6ba8e11beaa..ea8aca87800 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1051,7 +1051,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