码迷,mamicode.com
首页 > 编程语言 > 详细

python多线程生成缩略图

时间:2016-06-07 19:26:53      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

在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

python多线程生成缩略图

标签:

原文地址:http://www.cnblogs.com/gide/p/5568059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!