Is it possible to get image size before/without downloading?

Peter Otten __peter__ at web.de
Sat Jul 22 04:58:09 EDT 2006


aldonnelley at gmail.com wrote:

> Hi there: a bit of a left-field question, I think.
> I'm writing a program that analyses image files downloaded with a basic
> crawler, and it's slow, mainly because I only want to analyse files
> within a certain size range, and I'm having to download all the files
> on the page, open them, get their size, and then only analyse the ones
> that are in that size range.
> Is there a way (in python, of course!) to get the size of images before
> or without downloading them? I've checked around, and I can't seem to
> find anything promising...
> 
> Anybody got any clues?

The PIL can determine the size of an image from some "large enough" chunk at
the beginning of the image, e. g:

import Image
import urllib
from StringIO import StringIO

f = urllib.urlopen("http://www.python.org/images/success/nasa.jpg")
s = StringIO(f.read(512))
print Image.open(s).size

Peter



More information about the Python-list mailing list