Get image size wihout downloading it in Python
-
How can I get dimensions of image without actually downloading it? Is it even possible? I have a list of urls of images and I want to assign width and size to it. I know there is a way of doing it locally (http://stackoverflow.com/questions/1507084/how-to-check-dimensions-of-all-images-in-a-directory-using-python), but I don't want to download all the images. Edit: Following ed. suggestions, I edited the code. I came up with http://codepad.org/dC1RSwxi. Not sure weather it downloads whole file or just a part (as I wanted).
-
Answer:
If you're willing to download the first 24 bytes of each file, then http://code.google.com/p/bfg-pages/source/browse/trunk/pages/getimageinfo.py (mentioned in johnteslade's answer to the question you mention) will work out the dimensions. That's probably the least downloading necessary to do the job you want. import urllib2 start = urllib2.urlopen(image_url).read(24) Edit (1): In the case of jpeg files it seems to need more bytes. You could edit the function so that instead of reading a StringIO.StringIO(data) it instead reads the file handle from urlopen. Then it will read exactly as much of the image as it needs to find out the width and height.
grotos at Stack Overflow Visit the source
Other answers
This is based on ed's answer above, mixed with other things I found on the web. I ran into the same issue as grotos with .read(24). Download getimageinfo.py from https://code.google.com/p/bfg-pages/source/browse/trunk/pages/getimageinfo.py and download ReSeekFile.py from http://www.dalkescientific.com/writings/diary/archive/2005/05/18/updated_reseekfile.html. import urllib2 imgdata = urllib2.urlopen(href) image_type,width,height = getimageinfo.getImageInfo(imgdata) Modify getimageinfo as such... import ReseekFile def getImageInfo(datastream): datastream = ReseekFile.ReseekFile(datastream) data = str(datastream.read(30)) #Skipping to jpeg # handle JPEGs elif (size >= 2) and data.startswith('\377\330'): content_type = 'image/jpeg' datastream.seek(0) datastream.read(2) b = datastream.read(1) try: while (b and ord(b) != 0xDA): while (ord(b) != 0xFF): b = datastream.read(1) while (ord(b) == 0xFF): b = datastream.read(1) if (ord(b) >= 0xC0 and ord(b) <= 0xC3): datastream.read(3) h, w = struct.unpack(">HH", datastream.read(4)) break else: datastream.read(int(struct.unpack(">H", datastream.read(2))[0])-2) b = datastream.read(1) width = int(w) height = int(h) except struct.error: pass except ValueError: pass
jeffrg
It's not possible to do it directly, but there's a workaround for that. If the files are present on the server, then implement the API endpoint that takes image name as an argument and returns the size. But if the files are on the different server, you've got no other way but to download the files.
plaes
Related Q & A:
- How to add image to QTextBrowser using Python?Best solution by Stack Overflow
- How to resize image size in javascript?Best solution by Stack Overflow
- Why do I get a blank page when downloading?Best solution by Yahoo! Answers
- Where can I get free ebooks for downloading?Best solution by getfreeebooks.com
- How to get image from laptop to external monitor?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.