from PIL import Image

def stack_images_vertically(image1_path, image2_path, output_path):
    # 打开图片
    img1 = Image.open(image1_path)
    img2 = Image.open(image2_path)

    # 获取两张图片的宽度,以确保垂直拼接时宽度一致
    width = max(img1.width, img2.width)

    # 新建一张白色背景的图片,宽度为两者中最大的宽度
    new_img = Image.new(img1.mode, (width, img1.height + img2.height), color='white')

    # 将两张图片画到新建的图片上
    new_img.paste(img1, (0, 0))
    new_img.paste(img2, (0, img1.height))

    # 保存新图片
    new_img.save(output_path)

if __name__ == '__main__':
    # 使用函数拼接图片
    for i in range(1,80):
        stack_images_vertically(r'images2/{}.png'.format(i), r'images3/new.png', r'images3/{}.png'.format(i))