python中获取图片大小需要使用Pillow库中的img.size方法。
安装Pillow
pip install pillow
www#gaodaima.com来源gao!daima.com搞$代!码网搞代码
获取本地图片的大小:
import <a href="https://www.gaodaima.com/tag/os" title="查看更多关于os的文章" target="_blank">os</a> from PIL import Image <a href="https://www.gaodaima.com/tag/path" title="查看更多关于path的文章" target="_blank">path</a> = 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知识请关注搞代码网。
来源:搞代码网:原文地址:https://www.gaodaima.com