image from URL

Steve Holden sholden at holdenweb.com
Thu Oct 11 07:59:39 EDT 2001


[posted & mailed]

"Randy Heiland" <heiland at ncsa.uiuc.edu> wrote ...
> I'm trying to load/display an image from a URL, but can't seem to figure
> it out.  Have seen related threads on the list, but no final answer.
> Here's what I try:
>
> >>> import urllib, Image
> >>> fp = urllib.urlopen("http://www.python.org/pics/PyBanner027.gif")
> >>> im = Image.open(fp)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "/home/heiland/PythonMod/Imaging-1.1.2/PIL/Image.py", line 944,
> in open
>     fp.seek(0)
> AttributeError: addinfourl instance has no attribute 'seek'
>
>
> Can someone set me straight?

The Image.open() function appears to feel the need to move around in the
binray information stream. Hardly surprising, images being as complex as
they are. Reading the image content through urllib and then constructing a
StringIO ought to fix that. Hang on ...

> >>> import cStringIO # *much* faster than StringIO
>>> import urllib
>>> fp = urllib.urlopen("http://www.python.org/pics/PyBanner027.gif")
>>> img = cStringIO.StringIO(fp.read()) # constructs a StringIO holding the
image
>>> import Image # oh yes, we'll need that too!
>>> Image.open(img)
<GifImagePlugin.GifImageFile instance at 015184AC>
>>> # tadaaaaa!!

Seems to work ... don't forget to

    del img

if you need to save sapce and work on large images.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list