Added reading of J2K comments by radarhere · Pull Request #8622 · python-pillow/Pillow

#6909 added reading of JP2 comments. This adds reading of J2K comments as well.

JP2 comments are read from a "Contiguous codestream box" that "contains a valid and complete JPEG 2000 codestream"

if self.fp.read(12).endswith(b"jp2c\xff\x4f\xff\x51"):
self._parse_comment()

and starts by skipping hdr.

def _parse_comment(self) -> None:
hdr = self.fp.read(2)
length = _binary.i16be(hdr)
self.fp.seek(length - 2, os.SEEK_CUR)

Reading from a J2K image,

if sig == b"\xff\x4f\xff\x51":
self.codec = "j2k"
self._size, self._mode = _parse_codestream(self.fp)

the hdr is already read.

def _parse_codestream(fp: IO[bytes]) -> tuple[tuple[int, int], str]:
"""Parse the JPEG 2000 codestream to extract the size and component
count from the SIZ marker segment, returning a PIL (size, mode) tuple."""
hdr = fp.read(2)
lsiz = _binary.i16be(hdr)
siz = hdr + fp.read(lsiz - 2)

So this PR moves the hdr handling out of _parse_comment.