在img目录下7张图片

分别是

  11.jpg 22.jpg 33.jpg 44.jpg 55.jpg 66.jpg 77.jpg         

#encoding=utf-8
import os
import time
from multiprocessing.dummy import Pool as ThreadPool
from PIL import Image

SIZE = (75,75)
SAVE_DIRECTORY = 'thumbs'

def get_image_paths(folder):
    return(os.path.join(folder,f) for f in os.listdir(folder) if '.jpg' in f)

def create_thumbnail(filename):
    im = Image.open(filename) 
    im.thumbnail(SIZE, Image.ANTIALIAS) 
    base, fname = os.path.split(filename) 
    save_path = os.path.join(base, SAVE_DIRECTORY, fname) 
    im.save(save_path)


if __name__ == '__main__':

    start = time.time()
    folder = os.path.abspath('img')
    if not os.path.exists(os.path.join(folder,SAVE_DIRECTORY)):
        os.mkdir(os.path.join(folder,SAVE_DIRECTORY))    

    images = get_image_paths(folder)
    pool = ThreadPool()
    pool.map(create_thumbnail,images)
    pool.close()
    pool.join()
    end = time.time()
    print(end-start)

在对应的文件下会创建thumbs 缩略图文件,然后

生成对应的缩略图,执行时间为:

0.109212875366

文档更新时间: 2019-06-14 13:43   作者:月影鹏鹏