Python每日一题(第0005题)
题目:你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小。
代码:前提,必须安装PIL库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# -*- coding:utf-8 -*- ''' 批量修改文件中的图片为格式及大小 ''' import os, glob import Image path = raw_input("path:") width =int(raw_input("the width U want:")) imgslist = glob.glob(path+'/*.*') format = raw_input("format:") def small_img(): for imgs in imgslist: imgspath, ext = os.path.splitext(imgs) print imgs img = Image.open(imgs) (x,y) = img.size print x,y height =int( y * width /x) print height small_img =img.resize((width,height),Image.ANTIALIAS) small_img.save(imgspath +".thumbnail."+format) print "done" if __name__ == '__main__': small_img() |