diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index e5e8c85f4ed..20306580266 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -68,6 +68,13 @@ def test_sanity(self): assert im.format == "JPEG" assert im.get_format_mimetype() == "image/jpeg" + @pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0))) + def test_zero(self, size, tmp_path): + f = str(tmp_path / "temp.jpg") + im = Image.new("RGB", size) + with pytest.raises(ValueError): + im.save(f) + def test_app(self): # Test APP/COM reader (@PIL135) with Image.open(TEST_FILE) as im: diff --git a/Tests/test_image.py b/Tests/test_image.py index 9e90fc55315..07cf6eb92b3 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -656,7 +656,7 @@ def test_no_new_file_on_error(self, tmp_path): temp_file = str(tmp_path / "temp.jpg") im = Image.new("RGB", (0, 0)) - with pytest.raises(SystemError): + with pytest.raises(ValueError): im.save(temp_file) assert not os.path.exists(temp_file) diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index b9999bdaf7d..49c9379a870 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -632,6 +632,9 @@ def _save(im, fp, filename): except KeyError as e: raise OSError(f"cannot write mode {im.mode} as JPEG") from e + if im.width == 0 or im.height == 0: + raise ValueError("cannot write empty image as JPEG") + info = im.encoderinfo dpi = [round(x) for x in info.get("dpi", (0, 0))]