Skip to content

Commit aea8a55

Browse files
committed
Fix unexpected error when saving image with zero dimension
1 parent e2b87a0 commit aea8a55

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Tests/test_file_gif.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ def test_roundtrip_save_all_1(tmp_path: Path) -> None:
310310
assert reloaded.getpixel((0, 0)) == 255
311311

312312

313+
@pytest.mark.parametrize("size", ((0, 1), (1, 0), (0, 0)))
314+
def test_save_zero(size: tuple[int, int]) -> None:
315+
b = BytesIO()
316+
im = Image.new("RGB", size)
317+
with pytest.raises(SystemError):
318+
im.save(b, "GIF")
319+
320+
313321
@pytest.mark.parametrize(
314322
"path, mode",
315323
(

src/PIL/GifImagePlugin.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,13 @@ def _get_optimize(im: Image.Image, info: dict[str, Any]) -> list[int] | None:
937937
:param info: encoderinfo
938938
:returns: list of indexes of palette entries in use, or None
939939
"""
940-
if im.mode in ("P", "L") and info and info.get("optimize"):
940+
if (
941+
im.mode in ("P", "L")
942+
and info
943+
and info.get("optimize")
944+
and im.width != 0
945+
and im.height != 0
946+
):
941947
# Potentially expensive operation.
942948

943949
# The palette saves 3 bytes per color not used, but palette

0 commit comments

Comments
 (0)