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 (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 this code. Not sure weather it downloads whole file or just a part (as I wanted).
I found the solution on this site to work well:
If you're willing to download the first 24 bytes of each file, then this function (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.
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.
My fixed "getimageInfo.py", work with Python 3.4+, try it, just great!
Source code: http://code.google.com/p/bfg-pages/source/browse/trunk/pages/getimageinfo.py
I like this solution I found, which downloads chunks of the image until it can be recognized as an image file by PIL and then stops downloading. This ensures that enough of the image header gets downloaded to read the dimensions, but no more. (I found this here and here; I've adapted it for Python 3+.)
Since getimageinfo.py mentioned above doesn't work in Python3. Pillow is used instead of it.
Pillow can be found in pypi, or installed by using pip:
pip install pillow
.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.