Python把两张图片拼接为一张图片并保存

这里主要用Python扩展库pillow中Image对象的paste()方法把两张图片拼接起来

from os import listdir
from PIL import Image


def pinjie():
    # 获取当前文件夹中所有JPG图像
    im_list = [Image.open(fn) for fn in listdir() if fn.endswith('.jpg')]

    # 图片转化为相同的尺寸
    ims = []
    for i in im_list:
        new_img = i.resize((1280, 1280), Image.BILINEAR)
        ims.append(new_img)

    # 单幅图像尺寸
    width, height = ims[0].size

    # 创建空白长图
    result = Image.new(ims[0].mode, (width, height * len(ims)))

    # 拼接图片
    for i, im in enumerate(ims):
        result.paste(im, box=(0, i * height))

    # 保存图片
    result.save('res1.jpg')


if __name__ == '__main__':
    pinjie()

文件夹图片:

运行结果:

发布了374 篇原创文章 · 获赞 526 · 访问量 71万+

猜你喜欢

转载自blog.csdn.net/xun527/article/details/86577029
今日推荐