Python脚本批量修改照片大小

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

import Image, os
 
def resize(fname):
    img = Image.open(fname)
       
    ratio = float(img.size[0]) / img.size[1]
    width = int(ratio > 1 and 1024 or 1024 * ratio)
    height = int(ratio > 1 and 1024 / ratio or 1024)
    
    resized_img = img.resize((width, height), Image.ANTIALIAS)
    basename, extension = os.path.splitext(fname)
    resized_img.save(basename+'_resized.jpg')
 
if __name__ == '__main__':
    path = os.path.abspath(os.curdir)
    dirList = os.listdir(path);
    for fname in dirList:
        basename, extension = os.path.splitext(fname)
        if extension.lower() == '.jpg':
            resize(fname)


 

你可能感兴趣的:(python,照片,脚本,extension,图像处理,path)