Skip to content

Commit bd16568

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

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
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
(

Tests/test_file_pcx.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ def test_sanity(tmp_path: Path) -> None:
3737
im.save(f)
3838

3939

40+
@pytest.mark.parametrize("size", ((0, 1), (1, 0), (0, 0)))
41+
def test_save_zero(size: tuple[int, int]) -> None:
42+
b = io.BytesIO()
43+
im = Image.new("1", size)
44+
with pytest.raises(ValueError):
45+
im.save(b, "PCX")
46+
47+
4048
def test_p_4_planes() -> None:
4149
with Image.open("Tests/images/p_4_planes.pcx") as im:
4250
assert im.getpixel((0, 0)) == 3

Tests/test_file_spider.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ def test_save(tmp_path: Path) -> None:
6464
assert im2.format == "SPIDER"
6565

6666

67+
@pytest.mark.parametrize("size", ((0, 1), (1, 0), (0, 0)))
68+
def test_save_zero(size: tuple[int, int]) -> None:
69+
b = BytesIO()
70+
im = Image.new("1", size)
71+
with pytest.raises(SystemError):
72+
im.save(b, "SPIDER")
73+
74+
6775
def test_tempfile() -> None:
6876
# Arrange
6977
im = hopper()

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

src/PIL/PcxImagePlugin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ def _open(self) -> None:
146146

147147

148148
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
149+
if im.width == 0 or im.height == 0:
150+
msg = "Cannot write empty image as PCX"
151+
raise ValueError(msg)
152+
149153
try:
150154
version, bits, planes, rawmode = SAVE[im.mode]
151155
except KeyError as e:

src/PIL/SpiderImagePlugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def loadImageSeries(filelist: list[str] | None = None) -> list[Image.Image] | No
244244

245245
def makeSpiderHeader(im: Image.Image) -> list[bytes]:
246246
nsam, nrow = im.size
247-
lenbyt = nsam * 4 # There are labrec records in the header
247+
lenbyt = max(1, nsam) * 4 # There are labrec records in the header
248248
labrec = int(1024 / lenbyt)
249249
if 1024 % lenbyt != 0:
250250
labrec += 1

0 commit comments

Comments
 (0)