搜索
您的当前位置:首页正文

如何在python中获取调用图片的大小?

2024-07-16 来源:字库网

python中获取图片大小需要使用Pillow库中的img.size方法。

安装Pillow

pip install pillow

获取本地图片的大小:

import os
from PIL import Image

path = os.path.join(os.getcwd(),"23.png")
img = Image.open(path)

print img.format        # PNG
print img.size          # (3500, 3500)

获取远程图片的大小:

path = "http://h.hiphotos.baidu.com/image/pic/item/c8ea15ce36d3d5397966ba5b3187e950342ab0cb.jpg"

file = urllib2.urlopen(path)
tmpIm = cStringIO.StringIO(file.read())
img = Image.open(tmpIm)

print img.format         # JPEG
print img.size           # (801, 1200)

更多Python知识请关注。

Top