Skip to content

Commit ae8de77

Browse files
committed
Raise ValueError when trying to save empty image
1 parent 1bc0e1b commit ae8de77

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Tests/test_file_jpeg.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ def test_sanity(self):
6868
assert im.format == "JPEG"
6969
assert im.get_format_mimetype() == "image/jpeg"
7070

71+
@pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0)))
72+
def test_zero(self, size, tmp_path):
73+
f = str(tmp_path / "temp.jpg")
74+
im = Image.new("RGB", size)
75+
with pytest.raises(ValueError):
76+
im.save(f)
77+
7178
def test_app(self):
7279
# Test APP/COM reader (@PIL135)
7380
with Image.open(TEST_FILE) as im:

Tests/test_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ def test_no_new_file_on_error(self, tmp_path):
656656
temp_file = str(tmp_path / "temp.jpg")
657657

658658
im = Image.new("RGB", (0, 0))
659-
with pytest.raises(SystemError):
659+
with pytest.raises(ValueError):
660660
im.save(temp_file)
661661

662662
assert not os.path.exists(temp_file)

src/PIL/JpegImagePlugin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,8 @@ def get_sampling(im):
626626

627627

628628
def _save(im, fp, filename):
629+
if im.width == 0 or im.height == 0:
630+
raise ValueError("cannot write empty image as JPEG")
629631

630632
try:
631633
rawmode = RAWMODE[im.mode]

0 commit comments

Comments
 (0)