Python PIL库实现九宫格图片的裁剪与拼接方法

 更新时间:2026年05月28日 15:31:49   作者:detayun  
本文以抖音九宫格验证码为例,为大家详细详细介绍了如何通过Python的PIL库实现图片裁剪与拼接,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下

在验证码识别、图片预处理等场景中,经常需要从一张大图中按固定坐标裁出多张小图,再重新拼成一张新图。本文以抖音九宫格验证码为例,完整演示裁剪与拼接的实现。

一、需求拆解

原始图是一张九宫格大图,每张小图之间有间隙。目标:

  1. 按坐标裁出 9 张小图
  2. 每张缩放到 110×110
  3. 拼成一张 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
小图xy粘贴位置
(0,0)00(0, 0)
(0,1)10(110, 0)
(1,0)01(0, 110)
(2,2)22(220, 220)

四、常见坑

问题原因解决
s_img.size() 报错size 是属性不是方法去掉括号:s_img.size
拼接后有白边小图尺寸不一致确保 resize 统一为 110×110
缩放后模糊没加 LANCZOSresize(..., resample=Image.LANCZOS)
保存后质量差JPEG 默认 quality=75save(..., 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图片裁剪与拼接内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python 列表的清空方式

    Python 列表的清空方式

    今天小编就为大家分享一篇Python 列表的清空方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • python读取大文件越来越慢的原因与解决

    python读取大文件越来越慢的原因与解决

    这篇文章主要给大家介绍了关于python读取大文件越来越慢的原因与解决方法,文中通过示例代码介绍的非常详细,对大家学习或者使用Python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • Python批量为PDF添加水印的代码实现

    Python批量为PDF添加水印的代码实现

    有时候我们需要在PDF文件上添加水印,比如草稿、保密、审阅等标识,来提醒自己或他人,所以本文就教大家如何用Python给一大堆PDF文件添加水印,需要的朋友可以参考下
    2025-08-08
  • Python利用PyVista进行mesh的色彩映射的实现

    Python利用PyVista进行mesh的色彩映射的实现

    这篇文章主要介绍了Python利用PyVista进行mesh的色彩映射的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 关于查找numpy和pandas兼容的版本要求

    关于查找numpy和pandas兼容的版本要求

    本文主要介绍了关于查找numpy和pandas兼容的版本要求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2026-01-01
  • python倒序for循环实例

    python倒序for循环实例

    这篇文章主要介绍了python倒序for循环实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • 浅谈tensorflow模型保存为pb的各种姿势

    浅谈tensorflow模型保存为pb的各种姿势

    这篇文章主要介绍了浅谈tensorflow模型保存为pb的各种姿势,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • 用python实现名片管理系统

    用python实现名片管理系统

    这篇文章主要为大家详细介绍了用python实现名片管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • python中取绝对值简单方法总结

    python中取绝对值简单方法总结

    在本篇内容里小编给大家整理的是关于python中取绝对值简单方法,需要的朋友们可以学习下。
    2020-07-07
  • python编码最佳实践之总结

    python编码最佳实践之总结

    python编码最佳实践之总结,帮助大家整理了python编码最佳实践的相关知识点,重点从性能角度出发对python的一些惯用法做一个简单总结,感兴趣的小伙伴们可以参考一下
    2016-02-02

最新评论