Is there a function for Python which like getimagesize in PHP?

*爱你&永不变心* 提交于 2019-12-05 09:42:02

Using the python image library (PIL)

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

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

PHP can open a URL as it does a file. This could be a boon (as in your case), or a bane (as in remote file inclusion vulnerability).

Python opts to be explicit in that a file is a file, and a remote resource (URL, for example), is a remote one.

If you need some utility function to get image size from a remote resource, you probably need to write a wrapper to the local one. Usually you only need to read about 4096 bytes to determine the image size.

A little more work, yes, but there's no magic like in PHP.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!