Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ImageSequence all_frames #3778

Merged
merged 3 commits into from Jun 29, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions Tests/test_imagesequence.py
Expand Up @@ -74,3 +74,25 @@ def test_palette_mmap(self):
im.seek(0)
color2 = im.getpalette()[0:3]
self.assertEqual(color1, color2)

def test_all_frames(self):
# Test a single image
im = Image.open("Tests/images/iss634.gif")
ims = ImageSequence.all_frames(im)

self.assertEqual(len(ims), 42)
for i, im_frame in enumerate(ims):
self.assertFalse(im_frame is im)

im.seek(i)
self.assert_image_equal(im, im_frame)

# Test a series of images
ims = ImageSequence.all_frames([im, hopper(), im])
self.assertEqual(len(ims), 85)

# Test an operation
ims = ImageSequence.all_frames(im, lambda im_frame: im_frame.rotate(90))
for i, im_frame in enumerate(ims):
im.seek(i)
self.assert_image_equal(im.rotate(90), im_frame)
8 changes: 8 additions & 0 deletions docs/releasenotes/6.1.0.rst
Expand Up @@ -11,6 +11,14 @@ An optional ``include_layered_windows`` parameter has been added to ``ImageGrab.
defaulting to ``False``. If true, layered windows will be included in the resulting
image on Windows.

ImageSequence.all_frames
^^^^^^^^^^^^^^^^^^^^^^^^

A new method to facilitate applying a given function to all frames in an image, or to
all frames in a list of images. The frames are returned as a list of separate images.
For example, ``ImageSequence.all_frames(im, lambda im_frame: im_frame.rotate(90))``
could be used to return all frames from an image, each rotated 90 degrees.

Variation fonts
^^^^^^^^^^^^^^^

Expand Down
22 changes: 22 additions & 0 deletions src/PIL/ImageSequence.py
Expand Up @@ -54,3 +54,25 @@ def __next__(self):

def next(self):
return self.__next__()


def all_frames(im, func=None):
"""
Applies a given function to all frames in an image or a list of images.
The frames are returned as a list of separate images.

:param im: An image, or a list of images.
:param func: The function to apply to all of the image frames.
:returns: A list of images.
"""
if not isinstance(im, list):
im = [im]

ims = []
for imSequence in im:
current = imSequence.tell()

ims += [im_frame.copy() for im_frame in Iterator(imSequence)]

imSequence.seek(current)
return [func(im) for im in ims] if func else ims