python实现两张图片拼接

纵向拼接

from PIL import Image


def image_splicing(pic01, pic02):
    """
    图片拼接
    :param pic01: 图片1路径
    :param pic02: 图片2路径
    :return: 保存路径
    """
    with Image.open(pic01) as img_01, \
            Image.open(pic02) as img_02:
        img1_size, img2_size = img_01.size, img_02.size
        width = max([img1_size[0], img2_size[0]])
        height = img1_size[1] + img2_size[1]
        instance = Image.new('RGB', (width, height), (255, 255, 255))  # 创建背景为白色的空图片
        instance.paste(img_01)  # 以坐标(0,0)为基准粘贴第一张图片
        instance.paste(img_02, (0, img1_size[1]))  # 以坐标(0,第一张图片的高)为基准粘贴第二张图片
        # instance.show()
        save_path = 'D:/image_marge.png'
        instance.save(save_path)

        return save_path
posted @ 2022-07-08 11:27  Rong_Z  阅读(801)  评论(0编辑  收藏  举报