Skip to content

Commit 2756cd5

Browse files
authored
Merge pull request #8146 from Yay295/jp2_parsing
2 parents d2d03a1 + 88b21e7 commit 2756cd5

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

Tests/images/unknown_mode.j2k

318 Bytes
Binary file not shown.

Tests/test_file_jpeg2k.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ def test_issue_6194() -> None:
335335
assert im.getpixel((5, 5)) == 31
336336

337337

338+
def test_unknown_j2k_mode() -> None:
339+
with pytest.raises(UnidentifiedImageError):
340+
with Image.open("Tests/images/unknown_mode.j2k"):
341+
pass
342+
343+
338344
def test_unbound_local() -> None:
339345
# prepatch, a malformed jp2 file could cause an UnboundLocalError exception.
340346
with pytest.raises(UnidentifiedImageError):

src/PIL/Jpeg2KImagePlugin.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def next_box_type(self) -> bytes:
9797
return tbox
9898

9999

100-
def _parse_codestream(fp):
100+
def _parse_codestream(fp) -> tuple[tuple[int, int], str]:
101101
"""Parse the JPEG 2000 codestream to extract the size and component
102102
count from the SIZ marker segment, returning a PIL (size, mode) tuple."""
103103

@@ -122,7 +122,8 @@ def _parse_codestream(fp):
122122
elif csiz == 4:
123123
mode = "RGBA"
124124
else:
125-
mode = ""
125+
msg = "unable to determine J2K image mode"
126+
raise SyntaxError(msg)
126127

127128
return size, mode
128129

@@ -237,10 +238,6 @@ def _open(self) -> None:
237238
msg = "not a JPEG 2000 file"
238239
raise SyntaxError(msg)
239240

240-
if self.size is None or not self.mode:
241-
msg = "unable to determine size/mode"
242-
raise SyntaxError(msg)
243-
244241
self._reduce = 0
245242
self.layers = 0
246243

0 commit comments

Comments
 (0)