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


Posted in Python onJanuary 15, 2019

本文实例为大家分享了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'])

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
通过Python实现自动填写调查问卷
Sep 06 Python
高质量Python代码编写的5个优化技巧
Nov 16 Python
numpy添加新的维度:newaxis的方法
Aug 02 Python
python爬虫爬取微博评论案例详解
Mar 27 Python
Python统计一个字符串中每个字符出现了多少次的方法【字符串转换为列表再统计】
May 05 Python
对python中的装包与解包实例详解
Aug 24 Python
利用pandas向一个csv文件追加写入数据的实现示例
Apr 23 Python
python3.6.5基于kerberos认证的hive和hdfs连接调用方式
Jun 06 Python
python3实现飞机大战
Nov 29 Python
Python实现Kerberos用户的增删改查操作
Dec 14 Python
python 爬取天气网卫星图片
Jun 07 Python
python创建字典及相关管理操作
Apr 13 Python
python实现多张图片拼接成大图
Jan 15 #Python
解决新版Pycharm中Matplotlib图像不在弹出独立的显示窗口问题
Jan 15 #Python
python实现创建新列表和新字典,并使元素及键值对全部变成小写
Jan 15 #Python
Python数据可视化之画图
Jan 15 #Python
python实现在遍历列表时,直接对dict元素增加字段的方法
Jan 15 #Python
Python txt文件加入字典并查询的方法
Jan 15 #Python
Python XML转Json之XML2Dict的使用方法
Jan 15 #Python
You might like
php插入中文到sqlserver 2008里出现乱码的解决办法分享
2012/07/19 PHP
php mail to 配置详解
2014/01/16 PHP
php字符串分割函数用法实例
2015/03/17 PHP
php实现常见图片格式的水印和缩略图制作(面向对象)
2016/06/15 PHP
PHP _construct()函数讲解
2019/02/03 PHP
javascript 动态生成私有变量访问器
2009/12/06 Javascript
使用js判断TextBox控件值改变然后出发事件
2014/03/07 Javascript
javaScript使用EL表达式的几种方式
2014/05/27 Javascript
js从Cookies里面取值的简单实现
2014/06/30 Javascript
node.js适合游戏后台开发吗?
2014/09/03 Javascript
node.js中的url.resolve方法使用说明
2014/12/10 Javascript
JavaScript中用getDate()方法返回指定日期的教程
2015/06/09 Javascript
jstl中判断list中是否包含某个值的简单方法
2016/10/14 Javascript
Angular中自定义Debounce Click指令防止重复点击
2017/07/26 Javascript
深入理解ES6的迭代器与生成器
2017/08/19 Javascript
JavaScript数组的5种迭代方法
2017/09/29 Javascript
全新打包工具parcel零配置vue开发脚手架
2018/01/11 Javascript
nodejs爬虫初试superagent和cheerio
2018/03/05 NodeJs
解决iview多表头动态更改列元素发生的错误的方法
2018/11/02 Javascript
解决vuecli3中img src 的引入问题
2020/08/04 Javascript
[01:36:19]Secret vs NB 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
[01:38]完美世界高校联赛决赛花絮
2018/12/02 DOTA
Python多维/嵌套字典数据无限遍历的实现
2016/11/04 Python
Python获取指定文件夹下的文件名的方法
2018/02/06 Python
Django 缓存配置Redis使用详解
2019/07/23 Python
python3 实现爬取TOP500的音乐信息并存储到mongoDB数据库中
2019/08/24 Python
python如何实现不可变字典inmutabledict
2020/01/08 Python
python利用faker库批量生成测试数据
2020/10/15 Python
python代码实现图书管理系统
2020/11/30 Python
高清安全摄像头系统:Lorex Technology
2018/07/20 全球购物
校园学雷锋广播稿
2014/10/08 职场文书
继承公证书格式
2015/01/26 职场文书
员工自我工作评价
2015/03/06 职场文书
初中毕业感言300字
2015/07/31 职场文书
Redis基于Bitmap实现用户签到功能
2021/06/20 Redis
SpringBoot快速入门详解
2021/07/21 Java/Android