Skip to content

Commit

Permalink
Added reading of JPEG2000 comments
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jan 25, 2023
1 parent 698951e commit 8a9b598
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Binary file added Tests/images/comment.jp2
Binary file not shown.
5 changes: 5 additions & 0 deletions Tests/test_file_jpeg2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ def test_subsampling_decode(name):
assert_image_similar(im, expected, epsilon)


def test_comment():
with Image.open("Tests/images/comment.jp2") as im:
assert im.info["comment"] == b"Created by OpenJPEG version 2.5.0"


@pytest.mark.parametrize(
"test_file",
[
Expand Down
23 changes: 23 additions & 0 deletions src/PIL/Jpeg2KImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ def _open(self):
self._size, self.mode, self.custom_mimetype, dpi = header
if dpi is not None:
self.info["dpi"] = dpi
if self.fp.read(12).endswith(b"jp2c\xff\x4f\xff\x51"):
self._parse_comment()
else:
msg = "not a JPEG 2000 file"
raise SyntaxError(msg)
Expand Down Expand Up @@ -254,6 +256,27 @@ def _open(self):
)
]

def _parse_comment(self):
hdr = self.fp.read(2)
lsiz = struct.unpack(">H", hdr)[0]
siz = hdr + self.fp.read(lsiz - 2)

while True:
marker = self.fp.read(2)
if not marker:
break
typ = marker[1]
if typ in (0x90, 0xD9):
# Start of tile or end of codestream
break
hdr = self.fp.read(2)
length = struct.unpack(">H", hdr)[0]
data = hdr + self.fp.read(length - 2)
if typ == 0x64:
# Comment
self.info["comment"] = data[4:]
break

@property
def reduce(self):
# https://github.com/python-pillow/Pillow/issues/4343 found that the
Expand Down

0 comments on commit 8a9b598

Please sign in to comment.