用python将文件夹内的jpg图片全部拼接成长图,支持1亿像素

由于未提供博客具体内容,暂无法给出包含关键信息的摘要。
# -*- 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)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菌王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值