使用PIL模块,windows安装包下载:
http://www.pythonware.com/products/pil/
使用方法
# coding: utf8
# 获取指定图片的长和宽
from PIL import Image
img = Image.open("img.jpg")
print img.size
运行结果:
(52, 54)
Python获取图片长宽等信息的方法:
这段代码通过urllib2打开远程图片,通过cStringIO读取文件内容,不用保存到磁盘即可读取图片长宽等信息
8#!/usr/bin/env python
#encoding=utf-8
import cStringIO, urllib2, Image
url = 'http://www.jb51.net/images/logo.gif'
file = urllib2.urlopen(url)
tmpIm = cStringIO.StringIO(file.read()())
im = Image.open(tmpIm)
print im.format, im.size, im.mode
python 的pil模块有关于图片的API
import urllib.request as u
import io
from PIL import Image
#识别图像
def measure(img_url):
response = u.urlopen(img_url)
tmpIm = io.BytesIO(response.read())
im = Image.open(tmpIm)
print(im.format, im.size, im.mode)
if __name__ == '__main__':
img_url = "http://47.97.200.236:8097/5926f243203f2caf4c488e579264c7f5"
measure(img_url)