图片转换
# 图片转换
Pillow(PIL) 格式互转 JPEG↔PNG↔WebP、压缩质量、尺寸调整、缩略图、批量转换目录。
# 一、Pillow 基础
pip install Pillow
1
# 1.1 格式转换
#!/usr/bin/env python3
from PIL import Image
# ---- 单个转换 ----
img = Image.open('photo.png')
# PNG → JPEG
img_rgb = img.convert('RGB') # PNG 有透明通道,JPEG 需先转 RGB
img_rgb.save('photo.jpg', quality=85)
# PNG → WebP
img.save('photo.webp', quality=80)
# JPEG → PNG
img = Image.open('photo.jpg')
img.save('photo.png')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1.2 尺寸调整与缩略图
#!/usr/bin/env python3
from PIL import Image
img = Image.open('photo.jpg')
print(f"原始尺寸: {img.size}") # (4000, 3000)
# ---- 按宽度等比缩放 ----
w_percent = 800 / img.width
h_size = int(img.height * w_percent)
resized = img.resize((800, h_size), Image.LANCZOS)
resized.save('photo_800.jpg')
# ---- 缩略图(保持比例,裁剪到目标尺寸以内)----
img.thumbnail((300, 300))
img.save('thumb.jpg')
# ---- 裁剪——正方形头像 ----
min_dim = min(img.size)
left = (img.width - min_dim) // 2
top = (img.height - min_dim) // 2
img.crop((left, top, left + min_dim, top + min_dim)).save('avatar.jpg')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 1.3 图片压缩
#!/usr/bin/env python3
"""图片压缩——减小体积但保持可观画质"""
from PIL import Image
import os
def compress_image(input_path, output_path, max_size_kb=200, quality=85):
"""迭代压缩直到文件 < max_size_kb"""
img = Image.open(input_path)
if img.mode in ('RGBA', 'P'):
img = img.convert('RGB')
while quality > 10:
img.save(output_path, 'JPEG', quality=quality, optimize=True)
size_kb = os.path.getsize(output_path) / 1024
if size_kb <= max_size_kb:
break
quality -= 5
print(f"压缩完成: {os.path.getsize(input_path)/1024:.0f}KB → "
f"{os.path.getsize(output_path)/1024:.0f}KB (quality={quality})")
# compress_image('large.jpg', 'compressed.jpg', max_size_kb=200)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 二、批量处理
#!/usr/bin/env python3
"""批量图片格式转换 + 缩放——递归处理整个目录"""
from PIL import Image
from pathlib import Path
import sys, os
def batch_convert(input_dir, output_dir, out_format='webp',
max_width=1200, quality=80):
"""递归转换目录下所有图片"""
input_dir = Path(input_dir)
output_dir = Path(output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.gif', '.webp', '.tiff'}
files = [f for f in input_dir.rglob('*') if f.suffix.lower() in extensions]
for i, f in enumerate(files, 1):
rel = f.relative_to(input_dir)
out_path = output_dir / rel.with_suffix(f'.{out_format}')
out_path.parent.mkdir(parents=True, exist_ok=True)
img = Image.open(f)
if img.mode in ('RGBA', 'P') and out_format in ('jpg', 'jpeg'):
img = img.convert('RGB')
# 等比缩放
if img.width > max_width:
ratio = max_width / img.width
img = img.resize((max_width, int(img.height * ratio)),
Image.LANCZOS)
img.save(out_path, quality=quality, optimize=True)
print(f"[{i}/{len(files)}] {rel} → {out_path.relative_to(output_dir)}")
print(f"✅ 批量转换完成: {len(files)} 个文件")
if __name__ == '__main__':
batch_convert(sys.argv[1], sys.argv[2],
out_format=sys.argv[3] if len(sys.argv) > 3 else 'webp')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 三、高级操作
#!/usr/bin/env python3
from PIL import Image, ImageFilter, ImageEnhance, ImageDraw, ImageFont
import os
# ---- 添加水印 ----
def add_watermark(input_path, output_path, text="© Copyright"):
img = Image.open(input_path).convert('RGBA')
overlay = Image.new('RGBA', img.size, (0,0,0,0))
draw = ImageDraw.Draw(overlay)
# 使用默认字体(或指定 ttf 路径)
draw.text((20, img.height - 40), text, fill=(255,255,255,100))
combined = Image.alpha_composite(img, overlay)
combined.convert('RGB').save(output_path)
# ---- 模糊 / 锐化 / 亮度 ----
img = Image.open('photo.jpg')
img.filter(ImageFilter.GaussianBlur(5)).save('blur.jpg')
ImageEnhance.Contrast(img).enhance(1.5).save('contrast.jpg')
ImageEnhance.Brightness(img).enhance(1.2).save('bright.jpg')
# ---- 获取图片信息 ----
def image_info(path):
img = Image.open(path)
print(f"格式: {img.format} 尺寸: {img.size} 模式: {img.mode}")
print(f"文件大小: {os.path.getsize(path)/1024:.1f}KB")
print(f"DPI: {img.info.get('dpi', '未知')}")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
上次更新: 2026/06/17, 12:47:39