[Image-SIG] Re: Your wish is my command (long)

Fredrik Lundh fredrik@pythonware.com
Thu, 16 Jul 1998 18:43:02 +0100

>Implementation problem: I don't see how to read an image from a file stream
>object.  ImageFileIO isn't documented and I can't find the code.

(Sigh.  I really thought I'd eliminated all traces of ImageFileIO
from the docs...)

This question is a bit trickier than it sounds, since many formats
require random access to the image data.  Here's how I would do

 -- create a file-like class which wraps a file object, and
    provides read, seek, and tell methods.  this class should
    also keep track of the current file position.

 -- the read method calls "read" for the underlying file object,
    and updates the current position.

 -- the seek method returns immediately if the caller seeked
    to the current position.

    if the wrapped file object is seekable, call "seek" as usual
    and update the current position.

    if the wrapped file object is not seekable, skip data if the
    caller seeked beyond the current position, and raise a suitable
    exception otherwise.
 -- the tell method returns the current position.

Then wrap sys.stdin in an object of this kind, and pass it to
Image.open.  If PIL can read the image in streaming mode,
it will.  Otherwise, you'll get an exception.

Cheers /F