Python脚本批量修改照片大小

来源:互联网 发布:并行计算编程平台 编辑:程序博客网 时间:2024/05/22 10:32

旅游归来,照片太多了。不想把原大小的照片和人分享,又懒的安装什么图像处理软件,于是用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)


 

.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }