Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageSequence.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #
  15. ##
  16. class Iterator:
  17. """
  18. This class implements an iterator object that can be used to loop
  19. over an image sequence.
  20. You can use the ``[]`` operator to access elements by index. This operator
  21. will raise an :py:exc:`IndexError` if you try to access a nonexistent
  22. frame.
  23. :param im: An image object.
  24. """
  25. def __init__(self, im):
  26. if not hasattr(im, "seek"):
  27. msg = "im must have seek method"
  28. raise AttributeError(msg)
  29. self.im = im
  30. self.position = getattr(self.im, "_min_frame", 0)
  31. def __getitem__(self, ix):
  32. try:
  33. self.im.seek(ix)
  34. return self.im
  35. except EOFError as e:
  36. raise IndexError from e # end of sequence
  37. def __iter__(self):
  38. return self
  39. def __next__(self):
  40. try:
  41. self.im.seek(self.position)
  42. self.position += 1
  43. return self.im
  44. except EOFError as e:
  45. raise StopIteration from e
  46. def all_frames(im, func=None):
  47. """
  48. Applies a given function to all frames in an image or a list of images.
  49. The frames are returned as a list of separate images.
  50. :param im: An image, or a list of images.
  51. :param func: The function to apply to all of the image frames.
  52. :returns: A list of images.
  53. """
  54. if not isinstance(im, list):
  55. im = [im]
  56. ims = []
  57. for imSequence in im:
  58. current = imSequence.tell()
  59. ims += [im_frame.copy() for im_frame in Iterator(imSequence)]
  60. imSequence.seek(current)
  61. return [func(im) for im in ims] if func else ims