是否有像Python中的getimagesize一样的Python函数? [英] Is there a function for Python which like getimagesize in PHP?

查看:212
本文介绍了是否有像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

这篇关于是否有像Python中的getimagesize一样的Python函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆