From 09c190a1e654482c41426c0da68cfb1a1298159d Mon Sep 17 00:00:00 2001 From: Jeffrey 'Alex' Clark Date: Sun, 22 Nov 2026 22:31:33 -0500 Subject: [PATCH 1/2] Do not retain empty JPEG APP and COM segments in applist Co-Authored-By: Claude Opus 5.5 --- Tests/test_file_jpeg.py | 10 ++++++++++ docs/releasenotes/13.1.0.rst | 7 +++++++ src/PIL/JpegImagePlugin.py | 11 ++++++++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 1bc134e90aa..951346499c6 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -116,6 +116,16 @@ def test_app(self) -> None: assert im.info["comment"] == b"File written by Adobe Photoshop\xa8 4.0\x00" assert im.app["COM"] == im.info["comment"] + def test_empty_app_segments(self) -> None: + with open(TEST_FILE, "rb") as fp: + data = fp.read() + empty_segments = b"\xff\xe0\x00\x02" * 10 + b"\xff\xfe\x00\x02" * 10 + with Image.open(BytesIO(data[:2] + empty_segments + data[2:])) as im: + assert isinstance(im, JpegImagePlugin.JpegImageFile) + assert [name for name, _ in im.applist] == ["APP0", "COM"] + assert im.app["APP0"].startswith(b"JFIF") + im.load() + def test_comment_write(self) -> None: with Image.open(TEST_FILE) as im: assert im.info["comment"] == b"File written by Adobe Photoshop\xa8 4.0\x00" diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index 9bbc7a40ee4..6a92ceecef2 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -67,6 +67,13 @@ Two new image processing operations are available in ImageOps. Other changes ============= +Empty JPEG APP and COM segments +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Empty APP and COM segments in JPEG images are no longer added to +``JpegImageFile.applist``, since they carry no data. This reduces the memory used when +opening images that contain many of them. + Rounding crop coordinates ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index c740a45e2a0..25c192c91f6 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -70,6 +70,8 @@ # # Parser +_APP_NAMES = tuple(f"APP{i}" for i in range(16)) + def Skip(self: JpegImageFile, marker: int) -> None: assert self.fp is not None @@ -86,10 +88,12 @@ def APP(self: JpegImageFile, marker: int) -> None: n = i16(self.fp.read(2)) - 2 s = ImageFile._safe_read(self.fp, n) - app = f"APP{marker & 15}" + app = _APP_NAMES[marker & 15] self.app[app] = s # compatibility - self.applist.append((app, s)) + if s: + # Empty segments carry no data, so they are not retained + self.applist.append((app, s)) if marker == 0xFFE0 and s.startswith(b"JFIF"): # extract JFIF information @@ -193,7 +197,8 @@ def COM(self: JpegImageFile, marker: int) -> None: self.info["comment"] = s self.app["COM"] = s # compatibility - self.applist.append(("COM", s)) + if s: + self.applist.append(("COM", s)) def SOF(self: JpegImageFile, marker: int) -> None: From 4c6434d0de0b3314971cb69abbe935e879250009 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 23 Nov 2026 18:22:41 +1100 Subject: [PATCH 2/2] Use MARKER name value --- Tests/test_file_jpeg.py | 10 ++++++---- src/PIL/JpegImagePlugin.py | 25 ++++++++++--------------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 951346499c6..5e05c3631bb 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -16,6 +16,7 @@ ImageOps, JpegImagePlugin, UnidentifiedImageError, + _binary, features, ) @@ -119,12 +120,13 @@ def test_app(self) -> None: def test_empty_app_segments(self) -> None: with open(TEST_FILE, "rb") as fp: data = fp.read() - empty_segments = b"\xff\xe0\x00\x02" * 10 + b"\xff\xfe\x00\x02" * 10 - with Image.open(BytesIO(data[:2] + empty_segments + data[2:])) as im: + + # APP1 marker, zero data within + empty_marker = _binary.o16be(0xFFE1) + _binary.o16be(2) + + with Image.open(BytesIO(data[:2] + empty_marker + data[2:])) as im: assert isinstance(im, JpegImagePlugin.JpegImageFile) assert [name for name, _ in im.applist] == ["APP0", "COM"] - assert im.app["APP0"].startswith(b"JFIF") - im.load() def test_comment_write(self) -> None: with Image.open(TEST_FILE) as im: diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 25c192c91f6..835be9bc74a 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -70,16 +70,14 @@ # # Parser -_APP_NAMES = tuple(f"APP{i}" for i in range(16)) - -def Skip(self: JpegImageFile, marker: int) -> None: +def Skip(self: JpegImageFile, name: str, marker: int) -> None: assert self.fp is not None n = i16(self.fp.read(2)) - 2 ImageFile._safe_read(self.fp, n) -def APP(self: JpegImageFile, marker: int) -> None: +def APP(self: JpegImageFile, name: str, marker: int) -> None: # # Application marker. Store these in the APP dictionary. # Also look for well-known application markers. @@ -88,12 +86,9 @@ def APP(self: JpegImageFile, marker: int) -> None: n = i16(self.fp.read(2)) - 2 s = ImageFile._safe_read(self.fp, n) - app = _APP_NAMES[marker & 15] - - self.app[app] = s # compatibility + self.app[name] = s # compatibility if s: - # Empty segments carry no data, so they are not retained - self.applist.append((app, s)) + self.applist.append((name, s)) if marker == 0xFFE0 and s.startswith(b"JFIF"): # extract JFIF information @@ -188,7 +183,7 @@ def APP(self: JpegImageFile, marker: int) -> None: self.info["mpoffset"] = self.fp.tell() - n + 4 -def COM(self: JpegImageFile, marker: int) -> None: +def COM(self: JpegImageFile, name: str, marker: int) -> None: # # Comment marker. Store these in the APP dictionary. assert self.fp is not None @@ -201,7 +196,7 @@ def COM(self: JpegImageFile, marker: int) -> None: self.applist.append(("COM", s)) -def SOF(self: JpegImageFile, marker: int) -> None: +def SOF(self: JpegImageFile, name: str, marker: int) -> None: # # Start of frame marker. Defines the size and mode of the # image. JPEG is colour blind, so we use some simple @@ -230,7 +225,7 @@ def SOF(self: JpegImageFile, marker: int) -> None: msg = f"cannot handle {self.layers}-layer images" raise SyntaxError(msg) - if marker in [0xFFC2, 0xFFC6, 0xFFCA, 0xFFCE]: + if marker in {0xFFC2, 0xFFC6, 0xFFCA, 0xFFCE}: self.info["progressive"] = self.info["progression"] = 1 if self.icclist: @@ -250,7 +245,7 @@ def SOF(self: JpegImageFile, marker: int) -> None: self.layer.append((t[0], t[1] // 16, t[1] & 15, t[2])) -def DQT(self: JpegImageFile, marker: int) -> None: +def DQT(self: JpegImageFile, name: str, marker: int) -> None: # # Define quantization table. Note that there might be more # than one table in each marker. @@ -395,7 +390,7 @@ def _open(self) -> None: if i in MARKER: name, description, handler = MARKER[i] if handler is not None: - handler(self, i) + handler(self, name, i) if i == 0xFFDA: # start of scan rawmode = self.mode if self.mode == "CMYK": @@ -456,7 +451,7 @@ def draft( original_size = self.size assert isinstance(a, tuple) - if a[0] == "RGB" and mode in ["L", "YCbCr"]: + if a[0] == "RGB" and mode in {"L", "YCbCr"}: self._mode = mode a = mode, ""