Skip to content

Commit

Permalink
Raise ValueError when trying to save empty image
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Mar 28, 2022
1 parent 1bc0e1b commit adf9784
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Tests/test_file_jpeg.py
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions src/PIL/JpegImagePlugin.py
Expand Up @@ -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))]
Expand Down

0 comments on commit adf9784

Please sign in to comment.