Skip to content

Commit d8c8075

Browse files
authored
Merge pull request #7797 from radarhere/tga_palette
2 parents f543b03 + 818500b commit d8c8075

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

Tests/images/p_8.tga

18 Bytes
Binary file not shown.

Tests/test_file_tga.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pytest
99

10-
from PIL import Image
10+
from PIL import Image, UnidentifiedImageError
1111

1212
from .helper import assert_image_equal, assert_image_equal_tofile, hopper
1313

@@ -65,6 +65,11 @@ def roundtrip(original_im: Image.Image) -> None:
6565
roundtrip(original_im)
6666

6767

68+
def test_palette_depth_8(tmp_path: Path) -> None:
69+
with pytest.raises(UnidentifiedImageError):
70+
Image.open("Tests/images/p_8.tga")
71+
72+
6873
def test_palette_depth_16(tmp_path: Path) -> None:
6974
with Image.open("Tests/images/p_16.tga") as im:
7075
assert_image_equal_tofile(im.convert("RGB"), "Tests/images/p_16.png")
@@ -133,6 +138,11 @@ def test_small_palette(tmp_path: Path) -> None:
133138
assert reloaded.getpalette() == colors
134139

135140

141+
def test_missing_palette() -> None:
142+
with Image.open("Tests/images/dilation4.lut") as im:
143+
assert im.mode == "L"
144+
145+
136146
def test_save_wrong_mode(tmp_path: Path) -> None:
137147
im = hopper("PA")
138148
out = str(tmp_path / "temp.tga")

src/PIL/TgaImagePlugin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _open(self) -> None:
8585
elif depth == 16:
8686
self._mode = "LA"
8787
elif imagetype in (1, 9):
88-
self._mode = "P"
88+
self._mode = "P" if colormaptype else "L"
8989
elif imagetype in (2, 10):
9090
self._mode = "RGB"
9191
if depth == 32:
@@ -128,6 +128,9 @@ def _open(self) -> None:
128128
self.palette = ImagePalette.raw(
129129
"BGRA", b"\0" * 4 * start + self.fp.read(4 * size)
130130
)
131+
else:
132+
msg = "unknown TGA map depth"
133+
raise SyntaxError(msg)
131134

132135
# setup tile descriptor
133136
try:

0 commit comments

Comments
 (0)