IdentifiantMot de passe
Mot de passe oublié ?Je m'inscris ! (gratuit)
The ImageFile Module
Python Imaging Library Handbook

Prev   Next

The ImageFile Module

The ImageFile module provides support functions for the image open and save functions.

In addition, it provides a Parser class which can be used to decode an image piece by piece (e.g. while receiving it over a network connection). This class implements the same consumer interface as the standard sgmllib and xmllib modules.


Parse An Image
import ImageFile

fp = open("lena.pgm", "rb")

p = ImageFile.Parser()

while 1:
    s =
    if not s:

im = p.close()"copy.jpg")



ImageFile.Parser() => Parser instance

Creates a parser object. Parsers cannot be reused.




Feed a string of data to the parser. This method may raise an IOError exception.


parser.close() => image or None

Tells the parser to finish decoding. If the parser managed to decode an image, it returns an Image object. Otherwise, this method raises an IOError exception.

Note: If the file cannot be identified, the parser will raise an IOError exception in the close method. If the file can be identified, but not decoded (for example, if the data is damaged, or if it uses an unsupported compression method), the parser will raise an IOError exception as soon as possible, either in feed or close.