Skip to content

Commit 41a89ea

Browse files
authored
Merge pull request #8622 from radarhere/jpeg2000_comment
2 parents 1cf1932 + ad747f3 commit 41a89ea

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-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:

docs/releasenotes/11.1.0.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ zlib library, and what version of zlib-ng is being used::
5252
Other Changes
5353
=============
5454

55+
Reading JPEG 2000 comments
56+
^^^^^^^^^^^^^^^^^^^^^^^^^^
57+
58+
When opening a JPEG 2000 image, the comment may now be read into
59+
:py:attr:`~PIL.Image.Image.info` for J2K images, not just JP2 images.
60+
5561
zlib-ng in wheels
5662
^^^^^^^^^^^^^^^^^
5763

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)