由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)