Skip to content

Commit 7e208cc

Browse files
committed
Change to ValueError when encoding an empty image
1 parent e2b87a0 commit 7e208cc

File tree

4 files changed

+6
-6
lines changed

4 files changed

+6
-6
lines changed

Tests/test_file_jpeg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_sanity(self) -> None:
8585
def test_zero(self, size: tuple[int, int], tmp_path: Path) -> None:
8686
f = tmp_path / "temp.jpg"
8787
im = Image.new("RGB", size)
88-
with pytest.raises(ValueError):
88+
with pytest.raises(ValueError, match="cannot write empty image"):
8989
im.save(f)
9090

9191
def test_app(self) -> None:

Tests/test_file_libtiff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ def test_save_single_strip(
12441244
def test_save_zero(self, compression: str | None, tmp_path: Path) -> None:
12451245
im = Image.new("RGB", (0, 0))
12461246
out = tmp_path / "temp.tif"
1247-
with pytest.raises(SystemError):
1247+
with pytest.raises(ValueError, match="cannot write empty image"):
12481248
im.save(out, compression=compression)
12491249

12501250
def test_save_many_compressed(self, tmp_path: Path) -> None:

src/PIL/JpegImagePlugin.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,10 +661,6 @@ def get_sampling(im: Image.Image) -> int:
661661

662662

663663
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
664-
if im.width == 0 or im.height == 0:
665-
msg = "cannot write empty image as JPEG"
666-
raise ValueError(msg)
667-
668664
try:
669665
rawmode = RAWMODE[im.mode]
670666
except KeyError as e:

src/encode.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ _setimage(ImagingEncoderObject *encoder, PyObject *args) {
239239
if (!im) {
240240
return NULL;
241241
}
242+
if (im->xsize == 0 || im->ysize == 0) {
243+
PyErr_SetString(PyExc_ValueError, "cannot write empty image");
244+
return NULL;
245+
}
242246

243247
encoder->im = im;
244248

0 commit comments

Comments
 (0)