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

作者:修炼打怪的小乌龟 时间:2023-10-28 12:19:45 

本文实例为大家分享了python实现多张图片垂直合并的具体代码,供大家参考,具体内容如下


# 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'])

来源:https://blog.csdn.net/u010417185/article/details/52316326

标签:python,PIL,图片合并
0
投稿

猜你喜欢

  • CSS定位属性Position详解

    2009-09-16 20:37:00
  • Python socket套接字实现C/S模式远程命令执行功能案例

    2021-11-27 13:22:01
  • 基于python 凸包问题的解决

    2021-04-11 02:56:41
  • Nodejs之TCP服务端与客户端聊天程序详解

    2024-05-03 15:55:48
  • Python使用pytest-playwright的原因分析

    2023-12-12 01:22:39
  • python机器学习混淆矩阵及confusion matrix函数使用

    2023-03-08 18:14:12
  • 使用Python制作一个打字训练小工具

    2021-10-19 20:23:12
  • 详解Python_shutil模块

    2023-06-24 00:32:19
  • springboot 启动时初始化数据库的步骤

    2024-01-26 18:32:57
  • Python GUI和游戏开发从入门到实践

    2021-01-09 15:30:47
  • PHP依赖注入原理与用法分析

    2023-09-04 01:22:54
  • 如何实现论坛的树状记录表展开技术?

    2010-05-19 21:37:00
  • 读写json中文ASCII乱码问题的解决方法

    2023-02-13 02:32:25
  • Yahoo发布一款FireFox网站开发插件

    2007-09-23 16:11:00
  • Django缓存系统实现过程解析

    2021-07-22 23:18:09
  • mysql数据表的基本操作之表结构操作,字段操作实例分析

    2024-01-17 18:15:54
  • insert select与select into 的用法使用说明

    2012-01-05 18:47:58
  • Python try except finally资源回收的实现

    2021-04-05 20:53:06
  • Python转码问题的解决方法

    2023-06-30 07:48:52
  • 通过实例简单了解Python sys.argv[]使用方法

    2022-09-12 14:14:35
  • asp之家 网络编程 m.aspxhome.com