使用PIL实现多张图片垂直合并

本文介绍了一个简单的Python脚本,用于实现多张图片的垂直合并。该脚本支持调整图片大小并确保合并后的图片不会超出指定的最大宽度和高度限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# coding: utf-8  
# image_merge.py  
# 图片垂直合并  
# http://www.redicecn.com  
# redice@163.com  
  
import os  
import Image  
  
def image_resize(img, size=(1500, 1100)):  
    """调整图片大小 
    """  
    try:  
        if img.mode not in ('L', 'RGB'):  
            img = img.convert('RGB')  
        img = img.resize(size)  
    except Exception, e:  
        pass  
    return img  
  
def image_merge(images, output_dir='output', output_name='merge.jpg', \  
                restriction_max_width=None, restriction_max_height=None):  
    """垂直合并多张图片 
    images - 要合并的图片路径列表 
    ouput_dir - 输出路径 
    output_name - 输出文件名 
    restriction_max_width - 限制合并后的图片最大宽度,如果超过将等比缩小 
    restriction_max_height - 限制合并后的图片最大高度,如果超过将等比缩小 
    """  
    max_width = 0  
    total_height = 0  
    # 计算合成后图片的宽度(以最宽的为准)和高度  
    for img_path in images:  
        if os.path.exists(img_path):  
            img = Image.open(img_path)  
            width, height = img.size  
            if width > max_width:  
                max_width = width  
            total_height += height  
  
    # 产生一张空白图  
    new_img = Image.new('RGB', (max_width, total_height), 255)  
    # 合并  
    x = y = 0  
    for img_path in images:  
        if os.path.exists(img_path):  
            img = Image.open(img_path)  
            width, height = img.size  
            new_img.paste(img, (x, y))  
            y += height  
  
    if restriction_max_width and max_width >= restriction_max_width:  
        # 如果宽带超过限制  
        # 等比例缩小  
        ratio = restriction_max_height / float(max_width)  
        max_width = restriction_max_width  
        total_height = int(total_height * ratio)  
        new_img = image_resize(new_img, size=(max_width, total_height))  
  
    if restriction_max_height and total_height >= restriction_max_height:  
        # 如果高度超过限制  
        # 等比例缩小  
        ratio = restriction_max_height / float(total_height)  
        max_width = int(max_width * ratio)  
        total_height = restriction_max_height  
        new_img = image_resize(new_img, size=(max_width, total_height))  
      
    if not os.path.exists(output_dir):  
        os.makedirs(output_dir)  
    save_path = '%s/%s' % (output_dir, output_name)  
    new_img.save(save_path)  
    return save_path  
      
if __name__ == '__main__':  
    image_merge(images=['900-000-000-0501a_b.jpg', '900-000-000-0501b_b.JPG', '1216005237382a_b.jpg'])  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值