Python怎么批量拼接图片

   2025-01-31 8340
核心提示:在Python中,可以使用PIL(Python Imaging Library)库来进行图像处理和拼接。以下是一种批量拼接图片的方法:from PIL import I

在Python中,可以使用PIL(Python Imaging Library)库来进行图像处理和拼接。以下是一种批量拼接图片的方法:

from PIL import Imageimport osdef join_images(input_folder, output_file):    images = []    for filename in os.listdir(input_folder):        if filename.endswith(".jpg") or filename.endswith(".png"):            images.append(Image.open(os.path.join(input_folder, filename)))    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.width    new_image.save(output_file)# 使用示例input_folder = "path/to/input/folder/"output_file = "path/to/output/file.jpg"join_images(input_folder, output_file)

请将input_folder替换为包含要拼接图片的文件夹的路径,将output_file替换为输出拼接图片的文件路径。运行该函数后,会将文件夹中的所有图片按照水平方向拼接,并保存为指定的输出文件。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言