Python源码:同步绑定有exe文件,可下载直接使用
import os
from PIL import Image
def compress_images(input_folder, quality):
# 确定输出文件夹路径为输入路径同级的 "out"
output_folder = os.path.join(os.path.dirname(input_folder), "out")
# 确保输出文件夹存在
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 遍历输入文件夹中的所有文件
for root, dirs, files in os.walk(input_folder):
for file in files:
if file.lower().endswith((".png", ".jpg", ".jpeg", ".bmp", ".gif", ".webp")):
input_path = os.path.join(root, file)
output_path = os.path.join(output_folder, file)
try:
with Image.open(input_path) as img:
img = img.convert("RGB") # 确保所有图像是RGB格式
img.save(output_path, "JPEG", quality=quality)
print(f"Compressed: {input_path} → {output_path}")
except Excepti

最低0.47元/天 解锁文章
1549

被折叠的 条评论
为什么被折叠?



