Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/PIL/ImageSequence.py: 19%
30 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1#
2# The Python Imaging Library.
3# $Id$
4#
5# sequence support classes
6#
7# history:
8# 1997-02-20 fl Created
9#
10# Copyright (c) 1997 by Secret Labs AB.
11# Copyright (c) 1997 by Fredrik Lundh.
12#
13# See the README file for information on usage and redistribution.
14#
16##
19class Iterator:
20 """
21 This class implements an iterator object that can be used to loop
22 over an image sequence.
24 You can use the ``[]`` operator to access elements by index. This operator
25 will raise an :py:exc:`IndexError` if you try to access a nonexistent
26 frame.
28 :param im: An image object.
29 """
31 def __init__(self, im):
32 if not hasattr(im, "seek"):
33 raise AttributeError("im must have seek method")
34 self.im = im
35 self.position = getattr(self.im, "_min_frame", 0)
37 def __getitem__(self, ix):
38 try:
39 self.im.seek(ix)
40 return self.im
41 except EOFError as e:
42 raise IndexError from e # end of sequence
44 def __iter__(self):
45 return self
47 def __next__(self):
48 try:
49 self.im.seek(self.position)
50 self.position += 1
51 return self.im
52 except EOFError as e:
53 raise StopIteration from e
56def all_frames(im, func=None):
57 """
58 Applies a given function to all frames in an image or a list of images.
59 The frames are returned as a list of separate images.
61 :param im: An image, or a list of images.
62 :param func: The function to apply to all of the image frames.
63 :returns: A list of images.
64 """
65 if not isinstance(im, list):
66 im = [im]
68 ims = []
69 for imSequence in im:
70 current = imSequence.tell()
72 ims += [im_frame.copy() for im_frame in Iterator(imSequence)]
74 imSequence.seek(current)
75 return [func(im) for im in ims] if func else ims