Get image size without downloading it in Python

2019-02-01 10:55发布

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).

标签: python image url
8条回答
再贱就再见
2楼-- · 2019-02-01 11:38

Unfortunately I can't comment, so this is as an answer:

Use a get query with the header

"Range": "bytes=0-30"

And then simply use

http://code.google.com/p/bfg-pages/source/browse/trunk/pages/getimageinfo.py

If you use python's "requests", it's simply

r = requests.get(image_url, headers={
    "Range": "bytes=0-30"
})
image_info = get_image_info(r.content)

This fixes ed.'s answer and doesn't have any other dependencies (like ReSeekFile.py).

查看更多
Melony?
3楼-- · 2019-02-01 11:42

This is based on ed's answer mixed with other things I found on the web. I ran into the same issue as grotos with .read(24). Download getimageinfo.py from here and download ReSeekFile.py from here.

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
查看更多
登录 后发表回答