diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 1bc134e90aa..5e05c3631bb 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -16,6 +16,7 @@ ImageOps, JpegImagePlugin, UnidentifiedImageError, + _binary, features, ) @@ -116,6 +117,17 @@ 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() + + # 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"] + 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..835be9bc74a 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -71,13 +71,13 @@ # Parser -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. @@ -86,10 +86,9 @@ 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}" - - self.app[app] = s # compatibility - self.applist.append((app, s)) + self.app[name] = s # compatibility + if s: + self.applist.append((name, s)) if marker == 0xFFE0 and s.startswith(b"JFIF"): # extract JFIF information @@ -184,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 @@ -193,10 +192,11 @@ 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: +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 @@ -225,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: @@ -245,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. @@ -390,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": @@ -451,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, ""