Python PIL库实现九宫格图片的裁剪与拼接方法
作者:detayun
在验证码识别、图片预处理等场景中,经常需要从一张大图中按固定坐标裁出多张小图,再重新拼成一张新图。本文以抖音九宫格验证码为例,完整演示裁剪与拼接的实现。
一、需求拆解
原始图是一张九宫格大图,每张小图之间有间隙。目标:
- 按坐标裁出 9 张小图
- 每张缩放到 110×110
- 拼成一张 330×330 的新图
二、核心代码
from PIL import Image
img = Image.open(r"微信图片_20260528094122_13_1856.png")
# 九宫格坐标:3行 × 3列
rect_list = [
[(1,0,166,165), (181,0,346,165), (361,0,525,165)], # 第一行
[(1,180,166,345),(181,180,346,345),(361,180,525,345)], # 第二行
[(1,360,166,525),(181,360,346,525),(361,360,525,525)], # 第三行
]
# 创建 330×330 空白图(白色背景)
new_img = Image.new("RGB", (330, 330), (255, 255, 255))
for y in range(3):
for x in range(3):
# 1. 裁剪
s_img = img.crop(rect_list[y][x])
# 2. 高质量缩放(关键:加 LANCZOS)
s_img = s_img.resize((110, 110), resample=Image.LANCZOS)
# 3. 粘贴到新图(每张 110px,按网格排列)
new_img.paste(s_img, (x * 110, y * 110))
new_img.save("result.png")
三、关键细节
1. crop() 裁剪
crop() 接收一个四元组 (left, top, right, bottom):
s_img = img.crop((1, 0, 166, 165)) # 左上(1,0) 到 右下(166,165)
坐标从大图的 rect_list 中逐行读取。
2. resize() 高质量缩放
必须指定 resample=Image.LANCZOS,否则默认用 BILINEAR,缩放后模糊。
| 滤波器 | 质量 | 速度 |
|---|---|---|
| NEAREST | ⭐ | 最快 |
| BILINEAR | ⭐⭐ | 快 |
| BICUBIC | ⭐⭐⭐ | 中等 |
| LANCZOS | ⭐⭐⭐⭐⭐ | 较慢 |
PIL 10.0+ 写法:
s_img.resize((110, 110), resample=Image.Resampling.LANCZOS)
3. paste() 拼接
新图 330×330,每张小图 110×110,共 3×3 排列。粘贴坐标按网格计算:
paste_x = x * 110 paste_y = y * 110
| 小图 | x | y | 粘贴位置 |
|---|---|---|---|
| (0,0) | 0 | 0 | (0, 0) |
| (0,1) | 1 | 0 | (110, 0) |
| (1,0) | 0 | 1 | (0, 110) |
| (2,2) | 2 | 2 | (220, 220) |
四、常见坑
| 问题 | 原因 | 解决 |
|---|---|---|
s_img.size() 报错 | size 是属性不是方法 | 去掉括号:s_img.size |
| 拼接后有白边 | 小图尺寸不一致 | 确保 resize 统一为 110×110 |
| 缩放后模糊 | 没加 LANCZOS | resize(..., resample=Image.LANCZOS) |
| 保存后质量差 | JPEG 默认 quality=75 | save(..., quality=95) |
五、透明背景版本
如果需要透明底:
new_img = Image.new("RGBA", (330, 330), (0, 0, 0, 0)) # 透明背景
new_img.save("result.png") # 必须存 PNG
总结:crop 裁 → LANCZOS 缩 → paste 拼,三步搞定九宫格重排。
六、知识扩展
使用 Python 的 Pillow(PIL 的 fork 库)进行图片裁剪与拼接非常方便。下面分别介绍裁剪和拼接的方法,并给出完整示例。
安装 Pillow
pip install Pillow
图片裁剪(crop)
Image.crop(box) 方法用于裁剪图像,box 是一个四元组 (left, upper, right, lower),坐标基于原图左上角为原点。
from PIL import Image
# 打开图片
img = Image.open('input.jpg')
# 定义裁剪区域 (左, 上, 右, 下)
box = (100, 100, 400, 400) # 从(100,100)到(400,400)的正方形
cropped_img = img.crop(box)
# 保存裁剪结果
cropped_img.save('cropped.jpg')动态裁剪示例:裁剪图片的中心区域
width, height = img.size
crop_size = 300
left = (width - crop_size) // 2
top = (height - crop_size) // 2
right = left + crop_size
bottom = top + crop_size
center_crop = img.crop((left, top, right, bottom))
center_crop.save('center_crop.jpg')图片拼接(拼接)
拼接需要先创建一张足够大的空白画布,然后将各张图片粘贴上去。
1. 水平拼接(两张图并排)
# 打开两张图片
img1 = Image.open('pic1.jpg')
img2 = Image.open('pic2.jpg')
# 确保它们高度一致(可选)
if img1.height != img2.height:
# 将高者按比例缩放到与低者一致
new_height = min(img1.height, img2.height)
img1 = img1.resize((int(img1.width * new_height / img1.height), new_height))
img2 = img2.resize((int(img2.width * new_height / img2.height), new_height))
# 创建新画布,宽度为两张图宽度之和,高度取较大者
total_width = img1.width + img2.width
max_height = max(img1.height, img2.height)
new_img = Image.new('RGB', (total_width, max_height))
# 粘贴图片
new_img.paste(img1, (0, 0))
new_img.paste(img2, (img1.width, 0))
new_img.save('horizontal_merge.jpg')2. 垂直拼接(两张图上下堆叠)
img1 = Image.open('pic1.jpg')
img2 = Image.open('pic2.jpg')
total_height = img1.height + img2.height
max_width = max(img1.width, img2.width)
new_img = Image.new('RGB', (max_width, total_height))
new_img.paste(img1, (0, 0))
new_img.paste(img2, (0, img1.height))
new_img.save('vertical_merge.jpg')3. 多图网格拼接(例如 2x2)
images = [Image.open(f'pic{i}.jpg') for i in range(1, 5)] # 4张图
rows, cols = 2, 2
# 计算每个小图的尺寸(这里假设所有图片尺寸相同)
width, height = images[0].size
# 创建大图
grid_img = Image.new('RGB', (cols * width, rows * height))
for i, img in enumerate(images):
row = i // cols
col = i % cols
grid_img.paste(img, (col * width, row * height))
grid_img.save('grid_merge.jpg')4. 灵活拼接(列表 + 自动适应)
封装一个函数,支持任意数量的图片水平或垂直拼接:
def merge_images(image_paths, direction='horizontal'):
images = [Image.open(path) for path in image_paths]
if direction == 'horizontal':
total_width = sum(img.width for img in images)
max_height = max(img.height for img in images)
new_img = Image.new('RGB', (total_width, max_height))
x_offset = 0
for img in images:
new_img.paste(img, (x_offset, 0))
x_offset += img.width
elif direction == 'vertical':
total_height = sum(img.height for img in images)
max_width = max(img.width for img in images)
new_img = Image.new('RGB', (max_width, total_height))
y_offset = 0
for img in images:
new_img.paste(img, (0, y_offset))
y_offset += img.height
else:
raise ValueError("direction must be 'horizontal' or 'vertical'")
return new_img
# 使用
result = merge_images(['a.jpg', 'b.jpg', 'c.jpg'], 'horizontal')
result.save('merged.jpg')高级技巧:在拼接时添加边框或间距
可以在两张图片之间留白:
spacing = 10
total_width = img1.width + spacing + img2.width
new_img = Image.new('RGB', (total_width, max_height))
new_img.paste(img1, (0, 0))
new_img.paste(img2, (img1.width + spacing, 0))或者添加白色背景:
bg = Image.new('RGB', (total_width, max_height), color='white')
bg.paste(img1, (0, 0))
bg.paste(img2, (img1.width + spacing, 0))注意事项
图片模式:Image.open() 可能返回不同的模式(如 RGBA、RGB、L),拼接时建议统一转换为 RGB:
img = img.convert('RGB')性能:大量高清图片拼接时内存占用较大,可以考虑逐张处理或使用更底层的 numpy 加速。
透明背景:如果原图有透明通道(PNG),创建新画布时应使用 'RGBA' 模式。
完整示例:一键批量裁剪并拼接
假设有一个文件夹里的图片需要先裁剪中心区域,然后按 2x3 网格拼接:
import os
from PIL import Image
def batch_crop_and_merge(folder, crop_size, rows, cols, output_path):
# 获取所有图片
paths = [os.path.join(folder, f) for f in os.listdir(folder) if f.endswith(('.jpg', '.png'))]
images = []
for path in paths:
img = Image.open(path)
# 中心裁剪
w, h = img.size
left = (w - crop_size) // 2
top = (h - crop_size) // 2
cropped = img.crop((left, top, left + crop_size, top + crop_size))
images.append(cropped)
# 网格拼接
grid_img = Image.new('RGB', (cols * crop_size, rows * crop_size))
for idx, img in enumerate(images):
if idx >= rows * cols: break
x = (idx % cols) * crop_size
y = (idx // cols) * crop_size
grid_img.paste(img, (x, y))
grid_img.save(output_path)
print(f"已保存到 {output_path}")
# 调用
batch_crop_and_merge('./photos', crop_size=300, rows=2, cols=3, output_path='grid_result.jpg')到此这篇关于Python PIL库实现九宫格图片的裁剪与拼接方法的文章就介绍到这了,更多相关Python图片裁剪与拼接内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
