python3 批量缩放图片为iphone5的640*1136以下

try:
    from PIL import Image, ImageDraw, ImageFont, ImageEnhance
except ImportError:
    import Image, ImageDraw, ImageFont, ImageEnhance

import os

myPath = './srcimg/'
outPath = './destimg/'

def processImage(filesource,destsource,name,imgtype):
    '''
    :param filesource: 存放待转换图片的目录
    :param destsource: 存放转换输出后的图片目录
    :param name: 文件名
    :param imgtype: 文件类型
    :return:
    '''
    imgtype = 'jpeg' if imgtype == '.jpg' else 'png'

    im = Image.open(filesource+name)
    #缩放比例
    rate = max(im.size[0]/640.0 if im.size[0] > 640 else 0,im.size[1]/1136.0 if im.size[1] > 1136 else 0)
    if rate:
        im.thumbnail((im.size[0]/rate,im.size[1]/rate))
    im.save(destsource+name,imgtype)

def run():
    os.chdir(myPath)
    for i in os.listdir(os.getcwd()):
        print(i)
        postfix = os.path.splitext(i)[1]
        if postfix == '.jpg' or postfix == '.png':
            print(myPath,outPath,i,postfix)
            processImage('./','../'+outPath,i,postfix)
if __name__ == '__main__':
    # processImage(myPath, outPath, '1.jpg', '.jpg')
    run()
posted on 2017-09-05 21:00  神秘藏宝室  阅读(243)  评论(0编辑  收藏  举报

 >>>转载请注明出处<<<