批量修改图像的大小 Python PIL

#-*-coding:utf-8-*-
import os
import os.path
from PIL import Image
import time
def ResizeImage(filein, fileout, width, height, type):
    img = Image.open(filein)
    out = img.resize((width, height),Image.ANTIALIAS) #resize image with high-quality
    dir_path = str('/'.join(fileout.split('/')[:-1]))
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    out.save(fileout, type)


if __name__ == "__main__":
    filein = r'4__fenlei\test.png'

    for i in "23456789ABVDEFGHKMNPRSTUVWXYZ":
        target_path = "4__fenlei/" + i + "/"
        for title in os.listdir(target_path):
            print(title)
            filein = target_path + title
            print(filein)

            fileout = filein.replace('4__fenlei','fenlei')
            width = 30
            height = 40
            type = 'png'
            ResizeImage(filein, fileout, width, height, type)
           

 

posted @ 2017-07-27 21:06  淋哥  阅读(941)  评论(0)    收藏  举报