python小工具-批量压缩图片

通过PIL简单的批量压缩图片

import os
import time
from PIL import Image
from docx import Document
from docx.shared import Inches

rootpath = os.getcwd()
abspath = os.path.dirname(rootpath)
dir_picture = abspath+'\pictures'
a = os.chdir(dir_picture)
picdir_list = []
for i in os.listdir(dir_picture):
    pic_dir = dir_picture +'\\'+i
    for pic in os.listdir(pic_dir):
        im = Image.open(os.path.join(pic_dir,pic))
        w,h = im.size
        if w*h > 665000:
            im.thumbnail((w//2, h//2))
            os.chdir(pic_dir)
            im.save(os.path.join(pic_dir,pic))
print('图片压缩处理完成')

用法在桌面创建pictures和demo 2个文件夹,将要压缩的图片扔进入pictures文件夹。py文件扔进demo文件夹。然后执行即可

你可能感兴趣的:(小工具)