diff --git a/Tests/test_file_tga.py b/Tests/test_file_tga.py index 7ec562342e1..82bcf48caba 100644 --- a/Tests/test_file_tga.py +++ b/Tests/test_file_tga.py @@ -179,10 +179,20 @@ def test_save_wrong_mode(tmp_path: Path) -> None: im = hopper("PA") out = tmp_path / "temp.tga" - with pytest.raises(OSError): + with pytest.raises(OSError, match="cannot write mode PA as TGA"): im.save(out) +def test_save_1_mode_rle(tmp_path: Path) -> None: + im = Image.new("1", (1, 1)) + out = tmp_path / "temp.tga" + + with pytest.raises( + OSError, match="cannot write mode 1 as TGA with run-length encoding" + ): + im.save(out, compression="tga_rle") + + def test_save_mapdepth() -> None: # This image has been manually hexedited from 200x32_p_bl_raw.tga # to include an origin diff --git a/src/PIL/TgaImagePlugin.py b/src/PIL/TgaImagePlugin.py index b2989a4b764..8fd63b1df5b 100644 --- a/src/PIL/TgaImagePlugin.py +++ b/src/PIL/TgaImagePlugin.py @@ -205,6 +205,10 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: compression = im.encoderinfo.get("compression", im.info.get("compression")) rle = compression == "tga_rle" if rle: + if im.mode == "1": + msg = f"cannot write mode {im.mode} as TGA with run-length encoding" + raise OSError(msg) + imagetype += 8 id_section = im.encoderinfo.get("id_section", im.info.get("id_section", ""))