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 相关文章推荐
paramiko模块安装和使用(远程登录服务器)
Jan 27 Python
Python编程实现的简单神经网络算法示例
Jan 26 Python
python 把文件中的每一行以数组的元素放入数组中的方法
Apr 29 Python
Python 类的特殊成员解析
Jun 20 Python
python进行TCP端口扫描的实现
Dec 21 Python
PyQt弹出式对话框的常用方法及标准按钮类型
Feb 27 Python
教你一步步利用python实现贪吃蛇游戏
Jun 27 Python
Python中请不要再用re.compile了
Jun 30 Python
如何关掉pycharm中的python console(图解)
Oct 31 Python
python三引号如何输入
Jul 06 Python
Selenium及python实现滚动操作多种方法
Jul 21 Python
深入了解Python 变量作用域
Jul 24 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验证码函数的使用示例
2013/05/03 PHP
jquery获取多个checkbox的值异步提交给php的方法
2015/06/24 PHP
Laravel给生产环境添加监听事件(SQL日志监听)
2017/06/19 PHP
PHP接口类(interface)的定义、特点和应用示例
2020/05/18 PHP
Gambit vs CL BO3 第二场 2.13
2021/03/10 DOTA
JQuery团队打造的javascript单元测试工具QUnit介绍
2010/02/26 Javascript
jquery 图片 上一张 下一张 链接效果(续篇)
2010/04/20 Javascript
js实现一个省市区三级联动选择框代码分享
2013/03/06 Javascript
jquery表格内容筛选实现思路及代码
2013/04/16 Javascript
ExtJs纵坐标值重复问题的解决方法
2014/02/27 Javascript
js实现遮罩层划出效果是生成div而不是显示
2014/07/29 Javascript
JScript中的条件注释详解
2015/04/24 Javascript
纯JavaScript基于notie.js插件实现消息提示特效
2016/01/18 Javascript
jquery插件jquery.LightBox.js实现点击放大图片并左右点击切换效果(附demo源码下载)
2016/02/25 Javascript
深入理解关于javascript中apply()和call()方法的区别
2016/04/12 Javascript
jQuery插件Validation快速完成表单验证的方式
2016/07/28 Javascript
详解Js模板引擎(TrimPath)
2016/11/22 Javascript
BootStrap 实现各种样式的进度条效果
2016/12/07 Javascript
axios学习教程全攻略
2017/03/26 Javascript
javascript  数组排序与对象排序的实例
2017/07/17 Javascript
ReactNative 之FlatList使用及踩坑封装总结
2017/11/29 Javascript
OkHttp踩坑随笔为何 response.body().string() 只能调用一次
2018/01/08 Javascript
浅谈vuex中store的命名空间
2019/11/08 Javascript
jQuery事件模型默认行为执行顺序及trigger()与 triggerHandler()比较实例分析
2020/04/30 jQuery
react-intl实现React国际化多语言的方法
2020/09/27 Javascript
Python实现压缩与解压gzip大文件的方法
2016/09/18 Python
python使用minimax算法实现五子棋
2019/07/29 Python
PyTorch之图像和Tensor填充的实例
2019/08/18 Python
python实现简单猜单词游戏
2020/12/24 Python
处理textarea中的换行和空格
2019/12/12 HTML / CSS
Spartoo英国:欧洲最大的网上鞋店
2016/09/13 全球购物
全陪导游欢迎词
2014/01/17 职场文书
中学生教师节演讲稿
2014/09/03 职场文书
2014年电话客服工作总结
2014/12/09 职场文书
运动会开幕式通讯稿
2015/07/18 职场文书
mysql聚集索引、辅助索引、覆盖索引、联合索引的使用
2022/02/12 MySQL