# -*- coding:utf-8 -*-
from PIL import Image
import os
import re
path = 'your path'
quality = 75
newname = '_Q' + str(quality) + '.jpg'
for root, dirs, files in os.walk(path):
for name in files:
ext = os.path.splitext(name)[1]
if ext == '.jpg':
# 压缩图不做处理
if not re.search(r'_Q(\d)+\.jpg+', name):
# 已有压缩图不做处理
if not os.path.exists(os.path.join(root, name + newname)):
# 压缩
img = Image.open(os.path.join(root, name))
img.save(os.path.join(root, name + newname), quality=quality)
遍历目录并压缩图片
最新推荐文章于 2023-03-13 23:00:22 发布