From 5ad98e7abb19710cfb0c6c70ad52b543b1c5769b Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 13 Jan 2025 07:54:43 +1100 Subject: [PATCH 1/4] Moved get_child_images() --- src/PIL/Image.py | 46 ------------------------------------------ src/PIL/ImageFile.py | 48 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index dff3d063b..0648161be 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1553,52 +1553,6 @@ class Image: self._exif._loaded = False self.getexif() - def get_child_images(self) -> list[ImageFile.ImageFile]: - child_images = [] - exif = self.getexif() - ifds = [] - if ExifTags.Base.SubIFDs in exif: - subifd_offsets = exif[ExifTags.Base.SubIFDs] - if subifd_offsets: - if not isinstance(subifd_offsets, tuple): - subifd_offsets = (subifd_offsets,) - for subifd_offset in subifd_offsets: - ifds.append((exif._get_ifd_dict(subifd_offset), subifd_offset)) - ifd1 = exif.get_ifd(ExifTags.IFD.IFD1) - if ifd1 and ifd1.get(ExifTags.Base.JpegIFOffset): - assert exif._info is not None - ifds.append((ifd1, exif._info.next)) - - offset = None - for ifd, ifd_offset in ifds: - current_offset = self.fp.tell() - if offset is None: - offset = current_offset - - fp = self.fp - if ifd is not None: - thumbnail_offset = ifd.get(ExifTags.Base.JpegIFOffset) - if thumbnail_offset is not None: - thumbnail_offset += getattr(self, "_exif_offset", 0) - self.fp.seek(thumbnail_offset) - data = self.fp.read(ifd.get(ExifTags.Base.JpegIFByteCount)) - fp = io.BytesIO(data) - - with open(fp) as im: - from . import TiffImagePlugin - - if thumbnail_offset is None and isinstance( - im, TiffImagePlugin.TiffImageFile - ): - im._frame_pos = [ifd_offset] - im._seek(0) - im.load() - child_images.append(im) - - if offset is not None: - self.fp.seek(offset) - return child_images - def getim(self) -> CapsuleType: """ Returns a capsule that points to the internal image memory. diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index 5d0f87a9f..b2a44cb4d 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -36,7 +36,7 @@ import struct import sys from typing import IO, TYPE_CHECKING, Any, NamedTuple, cast -from . import Image +from . import ExifTags, Image from ._deprecate import deprecate from ._util import is_path @@ -163,6 +163,52 @@ class ImageFile(Image.Image): def _open(self) -> None: pass + def get_child_images(self) -> list[ImageFile]: + child_images = [] + exif = self.getexif() + ifds = [] + if ExifTags.Base.SubIFDs in exif: + subifd_offsets = exif[ExifTags.Base.SubIFDs] + if subifd_offsets: + if not isinstance(subifd_offsets, tuple): + subifd_offsets = (subifd_offsets,) + for subifd_offset in subifd_offsets: + ifds.append((exif._get_ifd_dict(subifd_offset), subifd_offset)) + ifd1 = exif.get_ifd(ExifTags.IFD.IFD1) + if ifd1 and ifd1.get(ExifTags.Base.JpegIFOffset): + assert exif._info is not None + ifds.append((ifd1, exif._info.next)) + + offset = None + for ifd, ifd_offset in ifds: + current_offset = self.fp.tell() + if offset is None: + offset = current_offset + + fp = self.fp + if ifd is not None: + thumbnail_offset = ifd.get(ExifTags.Base.JpegIFOffset) + if thumbnail_offset is not None: + thumbnail_offset += getattr(self, "_exif_offset", 0) + self.fp.seek(thumbnail_offset) + data = self.fp.read(ifd.get(ExifTags.Base.JpegIFByteCount)) + fp = io.BytesIO(data) + + with Image.open(fp) as im: + from . import TiffImagePlugin + + if thumbnail_offset is None and isinstance( + im, TiffImagePlugin.TiffImageFile + ): + im._frame_pos = [ifd_offset] + im._seek(0) + im.load() + child_images.append(im) + + if offset is not None: + self.fp.seek(offset) + return child_images + def get_format_mimetype(self) -> str | None: if self.custom_mimetype: return self.custom_mimetype From 34762ded7500a20f938f98250df6e650608cd57e Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 13 Jan 2025 07:57:28 +1100 Subject: [PATCH 2/4] Assert JpegIFByteCount is int --- src/PIL/ImageFile.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index b2a44cb4d..f905b34b6 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -191,7 +191,10 @@ class ImageFile(Image.Image): if thumbnail_offset is not None: thumbnail_offset += getattr(self, "_exif_offset", 0) self.fp.seek(thumbnail_offset) - data = self.fp.read(ifd.get(ExifTags.Base.JpegIFByteCount)) + + length = ifd.get(ExifTags.Base.JpegIFByteCount) + assert isinstance(length, int) + data = self.fp.read(length) fp = io.BytesIO(data) with Image.open(fp) as im: From a922126ed7495466b2193c6cf582ade11f0f8fe5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 13 Jan 2025 07:57:50 +1100 Subject: [PATCH 3/4] Assert fp is not None --- src/PIL/ImageFile.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index f905b34b6..3476e48ff 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -180,6 +180,7 @@ class ImageFile(Image.Image): ifds.append((ifd1, exif._info.next)) offset = None + assert self.fp is not None for ifd, ifd_offset in ifds: current_offset = self.fp.tell() if offset is None: From be8e55d28d3525b05769aee5f36b945bd6e01f77 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 17 Jan 2025 18:34:23 +1100 Subject: [PATCH 4/4] Added deprecation warning --- Tests/test_image.py | 5 ++++ docs/deprecations.rst | 10 +++++++ docs/releasenotes/11.2.0.rst | 58 ++++++++++++++++++++++++++++++++++++ docs/releasenotes/index.rst | 1 + src/PIL/Image.py | 6 ++++ src/PIL/ImageFile.py | 3 +- src/PIL/_deprecate.py | 2 ++ 7 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 docs/releasenotes/11.2.0.rst diff --git a/Tests/test_image.py b/Tests/test_image.py index fe43cea40..108013463 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -989,6 +989,11 @@ class TestImage: else: assert im.getxmp() == {"xmpmeta": None} + def test_get_child_images(self) -> None: + im = Image.new("RGB", (1, 1)) + with pytest.warns(DeprecationWarning): + assert im.get_child_images() == [] + @pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0))) def test_zero_tobytes(self, size: tuple[int, int]) -> None: im = Image.new("RGB", size) diff --git a/docs/deprecations.rst b/docs/deprecations.rst index 80966ca36..634cee689 100644 --- a/docs/deprecations.rst +++ b/docs/deprecations.rst @@ -183,6 +183,16 @@ ExifTags.IFD.Makernote ``ExifTags.IFD.Makernote`` has been deprecated. Instead, use ``ExifTags.IFD.MakerNote``. +Image.Image.get_child_images() +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. deprecated:: 11.2.0 + +``Image.Image.get_child_images()`` has been deprecated. and will be removed in Pillow +13 (2026-10-15). It will be moved to ``ImageFile.ImageFile.get_child_images()``. The +method uses an image's file pointer, and so child images could only be retrieved from +an :py:class:`PIL.ImageFile.ImageFile` instance. + Removed features ---------------- diff --git a/docs/releasenotes/11.2.0.rst b/docs/releasenotes/11.2.0.rst new file mode 100644 index 000000000..025b64660 --- /dev/null +++ b/docs/releasenotes/11.2.0.rst @@ -0,0 +1,58 @@ +11.2.0 +------ + +Security +======== + +TODO +^^^^ + +TODO + +:cve:`YYYY-XXXXX`: TODO +^^^^^^^^^^^^^^^^^^^^^^^ + +TODO + +Backwards Incompatible Changes +============================== + +TODO +^^^^ + +Deprecations +============ + +Image.Image.get_child_images() +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. deprecated:: 11.2.0 + +``Image.Image.get_child_images()`` has been deprecated. and will be removed in Pillow +13 (2026-10-15). It will be moved to ``ImageFile.ImageFile.get_child_images()``. The +method uses an image's file pointer, and so child images could only be retrieved from +an :py:class:`PIL.ImageFile.ImageFile` instance. + +API Changes +=========== + +TODO +^^^^ + +TODO + +API Additions +============= + +TODO +^^^^ + +TODO + +Other Changes +============= + +TODO +^^^^ + +TODO diff --git a/docs/releasenotes/index.rst b/docs/releasenotes/index.rst index bd8e5536f..be9f623d0 100644 --- a/docs/releasenotes/index.rst +++ b/docs/releasenotes/index.rst @@ -14,6 +14,7 @@ expected to be backported to earlier versions. .. toctree:: :maxdepth: 2 + 11.2.0 11.1.0 11.0.0 10.4.0 diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 0648161be..e512e6fc7 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1553,6 +1553,12 @@ class Image: self._exif._loaded = False self.getexif() + def get_child_images(self) -> list[ImageFile.ImageFile]: + from . import ImageFile + + deprecate("Image.Image.get_child_images", 13) + return ImageFile.ImageFile.get_child_images(self) # type: ignore[arg-type] + def getim(self) -> CapsuleType: """ Returns a capsule that points to the internal image memory. diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index 3476e48ff..93fb47874 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -180,8 +180,8 @@ class ImageFile(Image.Image): ifds.append((ifd1, exif._info.next)) offset = None - assert self.fp is not None for ifd, ifd_offset in ifds: + assert self.fp is not None current_offset = self.fp.tell() if offset is None: offset = current_offset @@ -210,6 +210,7 @@ class ImageFile(Image.Image): child_images.append(im) if offset is not None: + assert self.fp is not None self.fp.seek(offset) return child_images diff --git a/src/PIL/_deprecate.py b/src/PIL/_deprecate.py index 83952b397..9f9d8bbc9 100644 --- a/src/PIL/_deprecate.py +++ b/src/PIL/_deprecate.py @@ -47,6 +47,8 @@ def deprecate( raise RuntimeError(msg) elif when == 12: removed = "Pillow 12 (2025-10-15)" + elif when == 13: + removed = "Pillow 13 (2026-10-15)" else: msg = f"Unknown removal version: {when}. Update {__name__}?" raise ValueError(msg)