第 0005 题:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
#!/usr/bin python
#coding:utf-8
from PIL import Image
import os
def changesize(path):
for imgname in os.listdir(path):
#print imgname
imgpath = os.path.join(path,imgname)
#print imgpath
#with Image.open(imgpath) as im:
im = Image.open(imgpath)
width, height = im.size
#print width,height
if width >640 or height >1136:
th = width / 640
td = height / 1136
ts = max(th,td)
nw = int(width/ts)
nh = int(height/ts)
im = im.resize((nw,nh))
print 'finish_'+imgname.split('.')[0]+'.jpg'
im.save('finish_'+imgname.split('.')[0]+'.jpg')
print "The size of picture is %s,The width is %i,The heigth is %i" %(imgname,nw,nh)
else:
print "Don't change size of picture"
if __name__=="__main__":
changesize(r'F:\Python\img\img')
( 写于2016年4月26日,http://blog.csdn.net/bzd_111)