Skip to content

Commit dd410e4

Browse files
committed
Added reading of J2K comments
1 parent 0e3f51d commit dd410e4

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

Tests/test_file_jpeg2k.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,9 @@ def test_pclr() -> None:
424424

425425

426426
def test_comment() -> None:
427-
with Image.open("Tests/images/comment.jp2") as im:
428-
assert im.info["comment"] == b"Created by OpenJPEG version 2.5.0"
427+
for path in ("Tests/images/9bit.j2k", "Tests/images/comment.jp2"):
428+
with Image.open(path) as im:
429+
assert im.info["comment"] == b"Created by OpenJPEG version 2.5.0"
429430

430431
# Test an image that is truncated partway through a codestream
431432
with open("Tests/images/comment.jp2", "rb") as fp:

src/PIL/Jpeg2KImagePlugin.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ def _open(self) -> None:
252252
if sig == b"\xff\x4f\xff\x51":
253253
self.codec = "j2k"
254254
self._size, self._mode = _parse_codestream(self.fp)
255+
self._parse_comment()
255256
else:
256257
sig = sig + self.fp.read(8)
257258

@@ -262,6 +263,9 @@ def _open(self) -> None:
262263
if dpi is not None:
263264
self.info["dpi"] = dpi
264265
if self.fp.read(12).endswith(b"jp2c\xff\x4f\xff\x51"):
266+
hdr = self.fp.read(2)
267+
length = _binary.i16be(hdr)
268+
self.fp.seek(length - 2, os.SEEK_CUR)
265269
self._parse_comment()
266270
else:
267271
msg = "not a JPEG 2000 file"
@@ -296,10 +300,6 @@ def _open(self) -> None:
296300
]
297301

298302
def _parse_comment(self) -> None:
299-
hdr = self.fp.read(2)
300-
length = _binary.i16be(hdr)
301-
self.fp.seek(length - 2, os.SEEK_CUR)
302-
303303
while True:
304304
marker = self.fp.read(2)
305305
if not marker:

0 commit comments

Comments
 (0)