问题库 Python how-to

Python 如何批量压缩图片

快速答案

用 Pillow 打开图片、thumbnail 缩放,再保存到输出目录。

py
from PIL import Image
img = Image.open('input.jpg')
img.thumbnail((1200, 1200))
img.save('output.jpg', quality=85)

使用要点

  • 先用一小份测试数据跑通,再处理完整目录或正式数据。
  • 把路径、编码和输出文件名提到变量里,便于复用和排查。

相关内容