在Python中,可以使用PIL(Python Imaging Library)库来进行图像处理和拼接。以下是一种批量拼接图片的方法:
from PIL import Image import os def 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
替换为输出拼接图片的文件路径。运行该函数后,会将文件夹中的所有图片按照水平方向拼接,并保存为指定的输出文件。