批量获取图片宽高

#coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

from PIL import Image
import os
url=u"F:\这个文件夹有很多图片"
filename=u"F:\结果放在这里.txt"

def walkDir(rootdir):
    if os.path.isdir(rootdir):
        for item in os.listdir(rootdir):
            walkDir(rootdir+"\\"+item)
    else:
        try:
            img = Image.open(rootdir);
            width, height = img.size
            ctt=rootdir+"\t"+str(width)+"\t"+str(height)
            writeOneLIne(filename,ctt)
            print ctt
        except:
            pass

def writeOneLIne(filepath,content):
    f=open(filepath,'a')
    f.write(content)
    f.write("\n")
    f.close()

walkDir(url)
添加新评论