diff --git a/PIL/Image.py b/PIL/Image.py index ed1d10907..c643e24d9 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1715,7 +1715,10 @@ class Image(object): if not format: if ext not in EXTENSION: init() - format = EXTENSION[ext] + try: + format = EXTENSION[ext] + except KeyError: + raise ValueError('unknown file extension: {}'.format(ext)) if format.upper() not in SAVE: init() diff --git a/Tests/test_image.py b/Tests/test_image.py index 1bdbd3960..6aefc4f73 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -87,6 +87,11 @@ class TestImage(PillowTestCase): reloaded = Image.open(fp) self.assert_image_similar(im, reloaded, 20) + def test_unknown_extension(self): + im = hopper() + temp_file = self.tempfile("temp.unknown") + self.assertRaises(ValueError, lambda: im.save(temp_file)) + def test_internals(self): im = Image.new("L", (100, 100))