## The Python Imaging Library.# $Id$## sequence support classes## history:# 1997-02-20 fl Created## Copyright (c) 1997 by Secret Labs AB.# Copyright (c) 1997 by Fredrik Lundh.## See the README file for information on usage and redistribution.###from__future__importannotationsfromtypingimportCallablefrom.importImage
[docs]classIterator:""" This class implements an iterator object that can be used to loop over an image sequence. You can use the ``[]`` operator to access elements by index. This operator will raise an :py:exc:`IndexError` if you try to access a nonexistent frame. :param im: An image object. """def__init__(self,im:Image.Image)->None:ifnothasattr(im,"seek"):msg="im must have seek method"raiseAttributeError(msg)self.im=imself.position=getattr(self.im,"_min_frame",0)def__getitem__(self,ix:int)->Image.Image:try:self.im.seek(ix)returnself.imexceptEOFErrorase:msg="end of sequence"raiseIndexError(msg)fromedef__iter__(self)->Iterator:returnselfdef__next__(self)->Image.Image:try:self.im.seek(self.position)self.position+=1returnself.imexceptEOFErrorase:msg="end of sequence"raiseStopIteration(msg)frome
[docs]defall_frames(im:Image.Image|list[Image.Image],func:Callable[[Image.Image],Image.Image]|None=None,)->list[Image.Image]:""" 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. """ifnotisinstance(im,list):im=[im]ims=[]forimSequenceinim:current=imSequence.tell()ims+=[im_frame.copy()forim_frameinIterator(imSequence)]imSequence.seek(current)return[func(im)foriminims]iffuncelseims