# -*- coding: utf-8 -*-
from PIL import Image
import os
from datetime import datetime
# 当前文件夹的图片进行拼接生成新图片
# 获取当前文件夹路径
folder_path = os.getcwd()
# 定义目标长图的宽度
output_width = 0
# 遍历文件夹获取图片信息
images = []
for filename in os.listdir(folder_path):
if filename.endswith('.jpg') or filename.endswith('.png') or filename.endswith('.JPG') or filename.endswith('.PNG'):
img_path = os.path.join(folder_path, filename)
images.append(img_path)
with Image.open(img_path) as img:
output_width = max(output_width, img.width)
# 检查是否有图片
if not images:
raise ValueError("No images found in the folder")
# 计算长图的高度
output_height = sum(Image.open(img_path).height + 10 for img_path in images) - 10
# 创建目标长图
output_image = Image.new('RGB', (output_width, output_height), (255, 255, 255))
# 拼接图片
current_height = 0
for img_path in images:
with Image.open(img_path) as img:
output_image.paste(img, (0, current_height))
current_height += img.height + 10
# 获取当前日期和时间
current_datetime = datetime.now().strftime("%Y%m%d%H%M%S")
# 保存拼接后的长图,添加日期时分秒作为后缀
output_filename = f"output_{current_datetime}.jpg"
output_image.save(output_filename)
11-29
1115
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交