Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise ValueError when trying to save empty JPEG #6159

Merged
merged 1 commit into from Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion Tests/test_image.py
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/PIL/JpegImagePlugin.py
Expand Up @@ -626,6 +626,8 @@ def get_sampling(im):


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

try:
rawmode = RAWMODE[im.mode]
Expand Down