Reading image dimensions with PIL
Dave Brueck
dave at pythonapocrypha.com
Tue May 17 11:51:19 EDT 2005
Will McGugan wrote:
> I'm writing an app that downloads images. It rejects images that are
> under a certain size - whithout downloading them completely. I've
> implemented this using PIL, by downloading the first K and trying to
> create a PIL image with it. PIL raises an exception because the file is
> incomplete, but the image object is initialised with the image
> dimensions, which is what I need. It actualy works well enough, but I'm
> concerened about side-effects - since it seems an unconventional way of
> working with PIL. Can anyone see any problems with doing this? Or a
> better method?
If you're tossing images that are too _small_, is there any benefit to not
downloading the whole image, checking it, and then throwing it away?
Checking just the first 1K probably won't save you too much time unless you're
over a modem. Are you using a byte-range HTTP request to pull down the images or
just a normal GET (via e.g. urllib)? If you're not using a byte-range request,
then all of the data is already on its way so maybe you could go ahead and get
it all.
But hey, if your current approach works... :) It _is_ a bit unconventional, so
to reduce the risk you could test it on a decent mix of image types (normal
JPEG, progressive JPEG, normal & progressive GIF, png, etc.) - just to make sure
PIL is able to handle partial data for all different types you might encounter.
Also, if PIL can't handle the partial data, can you reliably detect that
scenario? If so, you could detect that case and use the
download-it-all-and-check approach as a failsafe.
-Dave
More information about the Python-list
mailing list