[Image-SIG] Re: How to open image file via HTTP and save locally?

Anastasios Hatzis ahatzis at ithcorp.com
Thu Apr 22 08:48:46 EDT 2004

On Wed, 2004-04-21 at 13:08, Fredrik Lundh wrote:
> Anastasios Hatzis wrote:
> > I want my Python script to read a remote image file on a web server
> > (e.g. JPG) and then save it locally.
> >
> > I imported PIL 1.1.4 on my Python 2.3.2 (Windows XP) and everything
> > works fine while I read locally stored image files, using the PIL.
> >
> > But I can't find an example or description, how to open an image file,
> > if it is not local but on a remote Web server ...
> > - neither in PIL handbook, Python website, nor in various mailing
> > archives.
> if you want PIL to open the file from the web:
>     import urllib
>     import Image
>     im = Image.open(urllib.urlopen(URL))
> if you just want to download the file to a local directory:
>     import urllib
>     urllib.urlretrieve(URL, FILENAME)
> see also:
>     http://www.python.org/doc/current/lib/module-urllib.html
>     http://effbot.org/zone/pil-image-size.htm
> </F>

Thank you for your help. Example 2 worked as described. And on 
your recommended effbot-link I found the information how to check 
size before downloading the image.

An error occured at example 1 (see below), that I could solve
with a suggestion of Steve Holden on 

I attached the error message and the solution below, 
so other users may find it on this list, if they got same problem.



Error of Example 2:
Traceback (most recent call last):
  File "F:\PYfiles\example1.py", line 4, in -toplevel-
    im = Image.open(urllib.urlopen('http://freegee.sourceforge.net/FG_EN/src/teasers_en/t_gee-power_en.gif'))
  File "F:\Python23\lib\site-packages\PIL\Image.py", line 1555, in open
AttributeError: addinfourl instance has no attribute 'seek'


Example Solution:

import cStringIO # *much* faster than StringIO
import urllib
import Image

file = urllib.urlopen('http://freegee.sourceforge.net/FG_EN/src/teasers_en/t_gee-power_en.gif')
im = cStringIO.StringIO(file.read()) # constructs a StringIO holding the image
img = Image.open(im)

# now use PIL
print img.format, img.size, img.mode

# delete img if you need to save space and work on large images
del img

More information about the Image-SIG mailing list