python把图片横向拼接成一张图片

由AI讯飞星火大模型辅助:

import os
from PIL import Image

def get_image_files(folder_path):
    image_files = []
    for file in os.listdir(folder_path):
        if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
            image_files.append(os.path.join(folder_path, file))
    return image_files

def sort_image_files(image_files):
    return sorted(image_files, key=lambda x: int(''.join(filter(str.isdigit, x.split('/')[-1].split('.')[0]))))

def merge_images(image_files, output_path):
    images = [Image.open(x) for x in image_files]
    widths, heights = zip(*(i.size for i in images))

    total_width = sum(widths)
    max_height = max(heights)

    new_image = Image.new('RGB', (total_width, max_height))

    x_offset = 0
    for image in images:
        # 纵向拼接图片
        new_image.paste(image, (x_offset, 0))
        x_offset += image.size[0]

    new_image.save(output_path)

if __name__ == '__main__':
    folder_path = '1' #这里是图片文件夹的位置(路径)-相对路径即可
    output_path = 'output/image.png'#这里是输出文件加的位置(路径)-相对路径即可

    image_files = get_image_files(folder_path)
    sorted_image_files = sort_image_files(image_files)
    merge_images(sorted_image_files, output_path)

版权声明:
作者:k, k
链接:http://kuyour.top/index.php/2023/10/12/python%e6%8a%8a%e5%9b%be%e7%89%87%e6%a8%aa%e5%90%91%e6%8b%bc%e6%8e%a5%e6%88%90%e4%b8%80%e5%bc%a0%e5%9b%be%e7%89%87/
来源:KuKey
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
海报
python把图片横向拼接成一张图片
由AI讯飞星火大模型辅助: import os from PIL import Image def get_image_files(folder_path): image_files = [] for file in os.listdir(fol……
<<上一篇
下一篇>>