Python3 使用PIL将图片切分为九宫格

效果为将一张图片经过填充后变为一张正方形的图片,然后切割为3*3的小图片

切割后的图片

转换步骤

1,新建一个正方形图片,边长是原图片的长宽最大值

    width, height = image.size
    new_len = max(width, height)

    # 将新图片是正方形,长度为原宽高中最长的
    new_image = Image.new(image.mode, (new_len, new_len), color='white')

2,根据两种不同的情况将原图片复制到正方形图片中,位置由左上端点位置确定

    if width > height:
        new_image.paste(image, (0, int((new_len - height) / 2)))
    else:
        new_image.paste(image, (int((new_len - width) / 2), 0))
    return new_image

3,图片分割,分割图片需要四个参数,x1,y1,x2,y2,分别表示分割区域的左上端点和右下端点

    width, height = image.size
    item_width = int(width / 3)

    # 保存每一个小切图的区域
    box_list = []

    for i in range(3):
        for j in range(3):
            # 切图区域是矩形,位置由对角线的两个点(左上和右下)确定
            box = (j * item_width, i * item_width, (j + 1) * item_width, (i + 1) * item_width)
            box_list.append(box)
    image_list = [image.crop(box) for box in box_list]

4,输出图片到指定路径

    for (index, image) in enumerate(image_list):
        image.save(f"{out_dir}/{index}.png", "PNG")

完整代码

from PIL import Image

import os


def fill_image(image):
    width, height = image.size
    new_len = max(width, height)

    # 将新图片是正方形,长度为原宽高中最长的
    new_image = Image.new(image.mode, (new_len, new_len), color='white')

    # 根据两种不同的情况,将原图片放入新建的空白图片中部
    if width > height:
        new_image.paste(image, (0, int((new_len - height) / 2)))
    else:
        new_image.paste(image, (int((new_len - width) / 2), 0))
    return new_image


def cut_image(image):
    width, height = image.size
    item_width = int(width / 3)

    # 保存每一个小切图的区域
    box_list = []

    for i in range(3):
        for j in range(3):
            # 切图区域是矩形,位置由对角线的两个点(左上和右下)确定
            box = (j * item_width, i * item_width, (j + 1) * item_width, (i + 1) * item_width)
            box_list.append(box)

    image_list = [image.crop(box) for box in box_list]
    return image_list


def save_images(image_list, out_dir):
    for (index, image) in enumerate(image_list):
        image.save(f"{out_dir}/{index}.png", "PNG")


def to9img(file_path, out_dir='.'):
    image = Image.open(file_path)
    image = fill_image(image)
    image_list = cut_image(image)
    save_images(image_list, out_dir)


if __name__ == '__main__':
    to9img('mix2s.png')

猜你喜欢

转载自my.oschina.net/ahaoboy/blog/1793680