Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Binary file added Tests/images/child_ifd.tiff
Binary file not shown.
Binary file added Tests/images/child_ifd_jpeg.tiff
Binary file not shown.
17 changes: 17 additions & 0 deletions Tests/test_file_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ def test_context_manager(self):
with Image.open("Tests/images/multipage.tiff") as im:
im.load()

def test_get_child_images(self):
def check(ims, sizes):
assert len(ims) == len(sizes)

for i, im in enumerate(ims):
w = sizes[i]
expected = Image.new("RGB", (w, w), "#f00")
assert_image_similar(im, expected, 1)

with Image.open("Tests/images/child_ifd.tiff") as im:
ims = im.get_child_images()
check(ims, (16, 8))

with Image.open("Tests/images/child_ifd_jpeg.tiff") as im:
ims = im.get_child_images()
check(ims, (20,))

def test_mac_tiff(self):
# Read RGBa images from macOS [@PIL136]

Expand Down
33 changes: 33 additions & 0 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,39 @@ def tell(self):
"""Return the current frame number"""
return self.__frame

def get_child_images(self):
if SUBIFD not in self.tag_v2:
return []
child_images = []
exif = self.getexif()
offset = None
for im_offset in self.tag_v2[SUBIFD]:
# reset buffered io handle in case fp
# was passed to libtiff, invalidating the buffer
Comment on lines +1157 to +1159
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from

# reset buffered io handle in case fp
# was passed to libtiff, invalidating the buffer
self.fp.tell()

current_offset = self._fp.tell()
if offset is None:
offset = current_offset

fp = self._fp
ifd = exif._get_ifd_dict(im_offset)
jpegInterchangeFormat = ifd.get(513)
if jpegInterchangeFormat is not None:
fp.seek(jpegInterchangeFormat)
jpeg_data = fp.read(ifd.get(514))

fp = io.BytesIO(jpeg_data)

with Image.open(fp) as im:
if jpegInterchangeFormat is None:
im._frame_pos = [im_offset]
im._seek(0)
im.load()
child_images.append(im)

if offset is not None:
self._fp.seek(offset)
return child_images

def getxmp(self):
"""
Returns a dictionary containing the XMP tags.
Expand Down
1 change: 1 addition & 0 deletions src/PIL/TiffTags.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def lookup(tag, group=None):
323: ("TileLength", LONG, 1),
324: ("TileOffsets", LONG, 0),
325: ("TileByteCounts", LONG, 0),
330: ("SubIFDs", LONG, 0),
332: ("InkSet", SHORT, 1),
333: ("InkNames", ASCII, 1),
334: ("NumberOfInks", SHORT, 1),
Expand Down