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 相关文章推荐
在Django的session中使用User对象的方法
Jul 23 Python
python实现给数组按片赋值的方法
Jul 28 Python
Python装饰器的执行过程实例分析
Jun 04 Python
python3使用matplotlib绘制散点图
Mar 19 Python
python微信聊天机器人改进版(定时或触发抓取天气预报、励志语录等,向好友推送)
Apr 25 Python
利用Python实现手机短信监控通知的方法
Jul 22 Python
Python paramiko模块使用解析(实现ssh)
Aug 30 Python
Python实现自定义读写分离代码实例
Nov 16 Python
Python numpy.zero() 初始化矩阵实例
Nov 27 Python
Python内置类型性能分析过程实例
Jan 29 Python
Python中常用的os操作汇总
Nov 05 Python
Python 类,对象,数据分类,函数参数传递详解
Sep 25 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
SONY ICF-SW07收音机电路分析
2021/03/02 无线电
PHP开启gzip页面压缩实例代码
2010/03/11 PHP
php数据库密码的找回的步骤
2011/01/12 PHP
采用thinkphp自带方法生成静态html文件详解
2014/06/13 PHP
php运行时动态创建函数的方法
2015/03/16 PHP
php实现有趣的人品测试程序实例
2015/06/08 PHP
php检查字符串中是否有外链的方法
2015/07/29 PHP
使用Entrust扩展包在laravel 中实现RBAC的功能
2020/03/16 PHP
PHP实现递归的三种方法
2020/07/04 PHP
JavaScript和ActionScript的交互实现代码
2010/08/01 Javascript
window.location 对象所包含的属性
2014/10/10 Javascript
jQuery源码分析之jQuery.fn.each与jQuery.each用法
2015/01/23 Javascript
jquery操作angularjs对象
2015/06/26 Javascript
jquery中的工具使用方法$.isFunction, $.isArray(), $.isWindow()
2015/08/09 Javascript
js实现的黑背景灰色二级导航菜单效果代码
2015/08/24 Javascript
基于Node.js的强大爬虫 能直接发布抓取的文章哦
2016/01/10 Javascript
jquery ajax结合thinkphp的getjson实现跨域的方法
2016/06/06 Javascript
jQuery插件WebUploader实现文件上传
2016/11/07 Javascript
JavaScript实现简单的树形菜单效果
2017/06/23 Javascript
Vue实现一个无限加载列表功能
2018/11/13 Javascript
详解Vue.js在页面加载时执行某个方法
2018/11/20 Javascript
微信小程序云开发 搭建一个管理小程序
2019/05/17 Javascript
微信小程序开发常见问题及解决方案
2019/07/11 Javascript
介绍Python的@property装饰器的用法
2015/04/28 Python
pandas.DataFrame 根据条件新建列并赋值的方法
2018/04/08 Python
浅谈python中str字符串和unicode对象字符串的拼接问题
2018/12/04 Python
python找出列表中大于某个阈值的数据段示例
2019/11/24 Python
TensorFLow 数学运算的示例代码
2020/04/21 Python
python else语句在循环中的运用详解
2020/07/06 Python
python如何操作mysql
2020/08/17 Python
jupyter 添加不同内核的操作
2021/02/06 Python
基于CSS3的CSS 多栏(Multi-column)实现瀑布流源码分享
2014/06/11 HTML / CSS
欧姆龙医疗欧洲有限公司:Omron Healthcare Europe B.V
2020/06/13 全球购物
合同权益转让协议书模板
2014/11/18 职场文书
百家讲坛观后感
2015/06/12 职场文书
Python 语言实现六大查找算法
2021/06/30 Python