Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ImageOps,
JpegImagePlugin,
UnidentifiedImageError,
_binary,
features,
)

Expand Down Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions docs/releasenotes/13.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
26 changes: 13 additions & 13 deletions src/PIL/JpegImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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, ""

Expand Down
Loading