diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index f818927f6a3..d78ed0401e2 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -85,7 +85,7 @@ def test_sanity(self) -> None: def test_zero(self, size: tuple[int, int], tmp_path: Path) -> None: f = tmp_path / "temp.jpg" im = Image.new("RGB", size) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="cannot write empty image"): im.save(f) def test_app(self) -> None: diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index c2336c05856..2ff5de4496b 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -1244,7 +1244,7 @@ def test_save_single_strip( def test_save_zero(self, compression: str | None, tmp_path: Path) -> None: im = Image.new("RGB", (0, 0)) out = tmp_path / "temp.tif" - with pytest.raises(SystemError): + with pytest.raises(ValueError, match="cannot write empty image"): im.save(out, compression=compression) def test_save_many_compressed(self, tmp_path: Path) -> None: diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 894c1547d7b..2f11cbfe313 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -661,10 +661,6 @@ def get_sampling(im: Image.Image) -> int: def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: - if im.width == 0 or im.height == 0: - msg = "cannot write empty image as JPEG" - raise ValueError(msg) - try: rawmode = RAWMODE[im.mode] except KeyError as e: diff --git a/src/encode.c b/src/encode.c index 513309c8d7d..7afe36cb3a0 100644 --- a/src/encode.c +++ b/src/encode.c @@ -239,6 +239,10 @@ _setimage(ImagingEncoderObject *encoder, PyObject *args) { if (!im) { return NULL; } + if (im->xsize == 0 || im->ysize == 0) { + PyErr_SetString(PyExc_ValueError, "cannot write empty image"); + return NULL; + } encoder->im = im;