2011年6月5日星期日

  Python脚本批量修改照片大小

旅游归来,照片太多了。不想把原大小的照片和人分享,又懒的安装什么图像处理软件,于是用python写个脚本批量处理一下,一蹴而就:

   1:  import Image, os
   2:   
   3:  def resize(fname):
   4:      img = Image.open(fname)
   5:         
   6:      ratio = float(img.size[0]) / img.size[1]
   7:      width = int(ratio > 1 and 1024 or 1024 * ratio)
   8:      height = int(ratio > 1 and 1024 / ratio or 1024)
   9:      
  10:      resized_img = img.resize((width, height), Image.ANTIALIAS)
  11:      basename, extension = os.path.splitext(fname)
  12:      resized_img.save(basename+'_resized.jpg')
  13:   
  14:  if __name__ == '__main__':
  15:      path = os.path.abspath(os.curdir)
  16:      dirList = os.listdir(path);
  17:      for fname in dirList:
  18:          basename, extension = os.path.splitext(fname)
  19:          if extension.lower() == '.jpg':
  20:              resize(fname)

没有评论:

发表评论