Skip to content
Related Articles

Related Articles

Python PIL | ImageSequence.Iterator()
  • Last Updated : 02 Aug, 2019

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageSequence module contains a wrapper class that lets you iterate over the frames of an image sequence.

ImageSequence.Iterator() 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 IndexError if you try to access a nonexistent frame.

Syntax: PIL.ImageSequence.Iterator(im)

Parameters:
im – An image object.

Returns: An Image object.



Image Used:




   
  
# importing Image class from PIL package 
from PIL import Image, ImageSequence
  
# creating a object 
im = Image.open(r"C:\Users\System-Pc\Desktop\home.png")
index = 1
for frame in ImageSequence.Iterator(im):
    frame.save("frame % d.png" % index)
    index = index + 1
  
im.getdata()
im.show()

Output:

Another Example:Here we use another image .jpg extension.

Image Used:




   
  
# importing Image class from PIL package 
from PIL import Image, ImageSequence
  
# creating a object 
im = Image.open(r"C:\Users\System-Pc\Desktop\tree.jpg")
index = 1
for frame in ImageSequence.Iterator(im):
    frame.save("frame % d.jpg" % index)
    index = index + 1
  
im.getdata()
im.show()

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :