python实现 -- 盆友圈九宫格

from PIL import Image

def cut_image(image):
    width, height = image.size
    item_width = width / 3.0
    item_height = height / 3.0

    box_list = []
    for row in range(0, 3):
        for col in range(0, 3):
            box = (col * item_width, row * item_height, (col + 1) * item_width, (row + 1) * item_height)

            box_list.append(box)

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

    return image_list


def save_images(image_list):

    index = 1
    for image in image_list:
        image.save('./codes' + str(index) + '.png', 'PNG')
        index += 1


if __name__ == '__main__':
    image = Image.open(r"20200916133850.png")
    image_list = cut_image(image)
    save_images(image_list)

  

原文地址:https://www.cnblogs.com/a438842265/p/13678694.html