FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Moved get_child_images() to ImageFile by radarhere · Pull Request #8689 · python-pillow/Pillow · GitHub

Moved get_child_images() to ImageFile - #8689

Merged
hugovk merged 4 commits into
python-pillow:mainfrom
radarhere:get_child_images
Jan 17, 2025
Merged

Moved get_child_images() to ImageFile#8689
hugovk merged 4 commits into
python-pillow:mainfrom
radarhere:get_child_images

Conversation

Copy link
Copy Markdown
Member

This is part of #8362 - I'm hoping to break down that PR into easier-to-review chunks.

get_child_images() relies on self.fp, and since that only exists for an ImageFile and not an Image, I suggest moving it there. This isn't a strictly necessary change, so feel free to disagree.

After that, I've also made changes to fix two mypy errors. One is asserting that fp is not None, like #8617. The other is asserting that an Exif tag is an integer, to fix

src/PIL/ImageFile.py:238: error: Argument 1 to "read" of "IO" has incompatible type "Optional[Any]"; expected "int" [arg-type]
data = self.fp.read(ifd.get(ExifTags.Base.JpegIFByteCount))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

hugovk commented Jan 16, 2025

Copy link
Copy Markdown
Member

get_child_images() relies on self.fp, and since that only exists for an ImageFile and not an Image, I suggest moving it there. This isn't a strictly necessary change, so feel free to disagree.

I wonder, are there some circumstances where this could be a breaking change?

Copy link
Copy Markdown
Member Author

Images that are not ImageFiles

>>> from PIL import Image
>>> im = Image.new("RGB", (1, 1))
>>> im.get_child_images()
[]

would start failing.

>>> im.get_child_images()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Image' object has no attribute 'get_child_images'

However, I would suggest that is more helpful than an apparently-successful empty list, since an Image by itself will never return any child images - for example, if I open an image as an ImageFile, and then copy() to turn it into an Image, there is an error.

>>> from PIL import Image
>>> im = Image.open("Tests/images/flower.jpg")
>>> im.get_child_images()
[<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=160x120 at 0x103ACA970>]
>>> im.copy().get_child_images()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PIL/Image.py", line 1574, in get_child_images
    current_offset = self.fp.tell()
AttributeError: 'Image' object has no attribute 'fp'

That would now change to

>>> im.copy().get_child_images()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Image' object has no attribute 'get_child_images'

which again, I think is more helpful.

hugovk commented Jan 16, 2025

Copy link
Copy Markdown
Member

Yes okay, if you'd like to go ahead with this, I suggest a deprecation cycle, so we don't break things without warning.

radarhere added the Deprecation Feature that will be removed in the future label Jan 17, 2025

Copy link
Copy Markdown
Member Author

Ok, I've deprecated the functionality, to be removed in Pillow 13.

hugovk merged commit 4f7510b into python-pillow:main Jan 17, 2025

hugovk commented Jan 17, 2025

Copy link
Copy Markdown
Member

Thanks!

radarhere deleted the get_child_images branch January 17, 2025 20:47
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Deprecation Feature that will be removed in the future

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL