Introduction to wrapper class in the ImageSequence module of Python image processing library PIL
The ImageSequence module contains a wrapper class that allows users to iteratively access each frame of the image sequence.
I. Functions of the ImageSequence Module
1. Iterator
Definition: ImageSequence. Iterator (image )? Iterator instance
Meaning: Create an iterator instance to allow users to access all frames in the sequence cyclically.
Example: See the following example.
Ii. ImageSequence Module Method
1. Operator
Definition: Operator []
Meaning: the user calls this operator and starts to access it from 0. When no other frame image exists, this iterator will generate an IndexError exception.
Example:
from PIL import Image, ImageSequenceim = Image.open("D:\\Code\\Python\\test\\img\\test01.gif")index = 1for frame in ImageSequence.Iterator(im): print "image: index %d, mode %s, size %s" % (index, frame.mode, frame.size) frame.save("frame%d.png" % index) index = index + 1iter = ImageSequence.Iterator(im)print "image 10: mode %s, size %s" % (iter[10].mode, iter[10].size)iter[10].show()
The image test01.gif contains multiple dynamic images. The ImageSequence. Iterator (im) function obtains the Iterator of the image object im, extracts each image one by one, and prints their modes and sizes. The corresponding image in the iterator is obtained through the operator. Iter [10] corresponds to the tenth image.
The program output is as follows:
Image: index 1, mode P, size (450,450)
Image: index 2, mode P, size (450,450)
Image: index 3, mode P, size (450,450)
Image: index 4, mode P, size (450,450)
Image: index 5, mode P, size (450,450)
Image: index 6, mode P, size (450,450)
Image: index 7, mode P, size (450,450)
Image: index 8, mode P, size (450,450)
Image: index 9, mode P, size (450,450)
Image: index 10, mode P, size (450,450)
Image: index 11, mode P, size (450,450)
Image: index 12, mode P, size (450,450)
Image: index 13, mode P, size (450,450)
Image: index 14, mode P, size (450,450)
Image: index 15, mode P, size (450,450)
Image: index 16, mode P, size (450,450)
Image: index 17, mode P, size (450,450)
Image: index 18, mode P, size (450,450)
Image: index 19, mode P, size (450,450)
Image: index 20, mode P, size (450,450)
Image: index 21, mode P, size (450,450)
Image: index 22, mode P, size (450,450)
Image: index 23, mode P, size (450,450)
Image10: mode P, size (450,450)
Figure test01.gif:
The second image is:
Of the 20th images:
The tenth image obtained using the operator [] is as follows: