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 判断一个进程是否存在
Apr 09 Python
python实现的一个火车票转让信息采集器
Jul 09 Python
Python MD5加密实例详解
Aug 02 Python
利用Python爬取微博数据生成词云图片实例代码
Aug 31 Python
python2.7读取文件夹下所有文件名称及内容的方法
Feb 24 Python
python用插值法绘制平滑曲线
Feb 19 Python
Python 通过requests实现腾讯新闻抓取爬虫的方法
Feb 22 Python
详解python持久化文件读写
Apr 06 Python
PyQt5 closeEvent关闭事件退出提示框原理解析
Jan 08 Python
Python collections.deque双边队列原理详解
Oct 05 Python
Python利器openpyxl之操作excel表格
Apr 17 Python
Python几种酷炫的进度条的方式
Apr 11 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 强制性文件下载功能的函数代码(任意文件格式)
2010/05/26 PHP
php实现二进制和文本相互转换的方法
2015/04/18 PHP
PHP借助phpmailer发送邮件
2015/05/11 PHP
在 Laravel 中 “规范” 的开发短信验证码发送功能
2017/10/26 PHP
thinkphp框架类库扩展操作示例
2019/11/26 PHP
如何用ajax来创建一个XMLHttpRequest对象
2012/12/10 Javascript
Node.js中使用Log.io在浏览器中实时监控日志(等同tail -f命令)
2014/09/17 Javascript
jQuery中JSONP的两种实现方式详解
2016/09/26 Javascript
jQuery实现ajax无刷新分页页码控件
2017/02/28 Javascript
JavaScript mixin实现多继承的方法详解
2017/03/30 Javascript
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#‘的解决方法
2017/06/17 Javascript
Javascript实现跨域后台设置拦截的方法详解
2017/08/04 Javascript
使用Fullpage插件快速开发整屏翻页的页面
2017/09/13 Javascript
JS文件中加载jquery.js的实例代码
2018/05/05 jQuery
Vee-validate 父组件获取子组件表单校验结果的实例代码
2019/05/20 Javascript
vue单文件组件无法获取$refs的问题
2020/06/24 Javascript
python计算一个序列的平均值的方法
2015/07/11 Python
Python的Flask框架标配模板引擎Jinja2的使用教程
2016/07/12 Python
Python Tkinter 简单登录界面的实现
2019/06/14 Python
Python 获取ftp服务器文件时间的方法
2019/07/02 Python
详解pycharm连接不上mysql数据库的解决办法
2020/01/10 Python
全天然狗零食:Best Bully Sticks
2016/09/22 全球购物
乐高西班牙官方商店:LEGO Shop ES
2019/12/01 全球购物
德国专业木制品经销商:Holz-Direkt24
2019/12/26 全球购物
Chi Chi London官网:购买连衣裙和礼服
2020/10/25 全球购物
汽车维修工岗位职责
2014/02/12 职场文书
2014年两会学习心得体会
2014/03/17 职场文书
中层干部培训方案
2014/06/16 职场文书
幼儿教师师德师风自我剖析材料
2014/09/29 职场文书
房屋分割离婚协议书范本
2014/12/01 职场文书
百日宴上的祝酒词
2015/08/10 职场文书
教你怎么用Python实现多路径迷宫
2021/04/29 Python
ES6 解构赋值的原理及运用
2021/05/25 Javascript
mysql主从复制的实现步骤
2021/10/24 MySQL
Nginx源码编译安装过程记录
2021/11/17 Servers
oracle delete误删除表数据后如何恢复
2022/06/28 Oracle