本文目录导读:

Python脚本(最灵活,支持所有格式)
安装依赖
pip install Pillow
脚本示例(resize_images.py)
import os
from PIL import Image
def batch_resize(input_dir, output_dir, target_size=(800, 600)):
"""批量调整图片尺寸"""
os.makedirs(output_dir, exist_ok=True)
for filename in os.listdir(input_dir):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')):
img_path = os.path.join(input_dir, filename)
with Image.open(img_path) as img:
# 保持比例缩放
img.thumbnail(target_size, Image.LANCZOS)
# 或者直接强制调整大小
# img = img.resize(target_size, Image.LANCZOS)
save_path = os.path.join(output_dir, filename)
img.save(save_path, optimize=True)
print(f"已处理: {filename}")
# 使用示例
batch_resize("原始图片文件夹", "输出文件夹", (1200, 900))
高级版本(带压缩和质量控制)
from PIL import Image
import os, concurrent.futures
def resize_with_quality(src, dst, size=(1920, 1080), quality=85):
with Image.open(src) as img:
img.thumbnail(size, Image.LANCZOS)
img.save(dst, optimize=True, quality=quality)
def parallel_resize(input_dir, output_dir):
os.makedirs(output_dir, exist_ok=True)
files = [f for f in os.listdir(input_dir) if Image.open(os.path.join(input_dir, f)).verify()]
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = []
for f in files:
src = os.path.join(input_dir, f)
dst = os.path.join(output_dir, f)
futures.append(executor.submit(resize_with_quality, src, dst))
for future in concurrent.futures.as_completed(futures):
print(f"完成: {future.result()}")
命令行工具(适合Linux/Mac/WSL)
使用ImageMagick(最强大)
# 安装
sudo apt install imagemagick # Ubuntu/Debian
brew install imagemagick # Mac
# 批量缩放(保持比例)
mogrify -resize 800x600 -path ./output_dir *.jpg
# 强制调整尺寸
mogrify -resize 800x600! -path ./output_dir *.png
# 递归处理子文件夹
find . -name "*.jpg" -exec convert {} -resize 800x600 ../output/{} \;
使用sips(Mac自带)
# 批量缩放所有图片
for file in *.jpg; do
sips -Z 800 "$file" --out "${file%.jpg}_small.jpg"
done
# 指定宽高
sips --resampleWidth 800 --resampleHeight 600 *.jpg
跨平台工具
ImageMagick(Windows)
:: 在CMD中使用
mogirfy -resize 800x600 *.jpg
:: 或写一个批处理脚本
@echo off
for %%i in (*.jpg *.png) do (
convert "%%i" -resize 800x600 "output\%%i"
)
XnConvert(图形界面工具)
- 下载安装:XnConvert官网
- 操作步骤:
- 添加图片(可拖拽文件夹)
- 选择"变换" → "调整尺寸"
- 设置尺寸(如:1200 x 900)
- 选择"输出"格式和位置
- 点击转换
常用调整尺寸策略
| 策略 | 代码示例 | 说明 |
|---|---|---|
| 保持比例 | img.thumbnail((800,600)) |
自动保持长宽比 |
| 强制尺寸 | img.resize((800,600)) |
可能变形 |
| 按比例缩放 | img.resize((int(w*0.5), int(h*0.5))) |
缩小50% |
| 最短边对齐 | size=800; w,h=img.size; ratio=size/min(w,h); ... |
常见于缩略图 |
批量转码+调整尺寸(WebP格式)
from PIL import Image
import os
def convert_to_webp(input_dir, output_dir, size=(1200, 900)):
os.makedirs(output_dir, exist_ok=True)
for f in os.listdir(input_dir):
if f.lower().endswith(('.png', '.jpg', '.jpeg')):
img = Image.open(os.path.join(input_dir, f))
img.thumbnail(size, Image.LANCZOS)
webp_path = os.path.join(output_dir, f"{os.path.splitext(f)[0]}.webp")
img.save(webp_path, 'WEBP', quality=85)
print(f"已转换: {f} -> {webp_path}")
选择建议
- 小批量(<50张):用XnConvert图形界面最快
- 程序员/自动化:Python脚本最灵活
- Linux用户:ImageMagick一行命令搞定
- Mac用户:自带sips非常好用
- Web开发:推荐统一转成WebP格式
如果需要处理大量图片(数千张),建议使用Python的并行处理版本。