python递归地将文件夹中jpg图片生成缩略图并压缩

import os
import glob
import shutil
from PIL import Image

os.chdir("/Users/somebody/sport_game")
list_file = []
for root, dirs, files in os.walk("./"): #遍历文件夹
    jpgs = glob.glob("{}/*.jpg".format(root))
    if len(jpgs) > 0:
        list_file.extend(jpgs)
if not os.path.exists("./preview"):
    os.mkdir("./preview")
for jpg in list_file:
    img = Image.open(jpg)
    img.thumbnail((312, 312))
    pname, fname = os.path.split(jpg)
    img.save("./preview/" + "缩略图_{}".format(fname))
shutil.make_archive("imgs_preview", "zip", "./preview")


你可能感兴趣的:(python,开发语言,后端)