是否有像Python中的getimagesize一样的Python函数?

是否有像Python中的getimagesize一样的Python函数?

问题描述:

我已经搜索了一段时间,并且有一个函数调用get_image_dimensions(),但是,根据我的理解,它适用于下载或说本地的图像。那么,像PHP中的getimagesize这样的任何函数或解决方案,我们只能通过URL获取图像的维度,而不是通过本地路径获取?

I have search for a while, and there is a function call get_image_dimensions(), however, as to my understanding, it works for the images which are downloaded or say local. So, any functions or solution like getimagesize in PHP, that we can just get the dimension of an image via URL, instead of path to local?

使用python图像库(PIL)

Using the python image library (PIL)

from PIL import Image
im = Image.open("fileName.jpg")
im.size

如果你有一个网址,请打开它通过urlopen并将文件对象传递给Image.open

If you have an url, open it via urlopen and pass the file object to Image.open

import urllib2 as urllib
fd = urllib.urlopen("http://a/b/c")
im = Image.open(fd)
im.size