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中的生成器和yield详细介绍
Jan 09 Python
简单学习Python time模块
Apr 29 Python
CentOS 6.X系统下升级Python2.6到Python2.7 的方法
Oct 12 Python
Python多进程并发与多线程并发编程实例总结
Feb 08 Python
python 判断网络连通的实现方法
Apr 22 Python
python爬虫实例详解
Jun 19 Python
Python画柱状统计图操作示例【基于matplotlib库】
Jul 04 Python
浅谈python 导入模块和解决文件句柄找不到问题
Dec 15 Python
python实现定时发送qq消息
Jan 18 Python
Python一键查找iOS项目中未使用的图片、音频、视频资源
Aug 12 Python
python3 requests库实现多图片爬取教程
Dec 18 Python
PyCharm 安装与使用配置教程(windows,mac通用)
May 12 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
在任意字符集下正常显示网页的方法一
2007/04/01 PHP
PHP Hash算法:Times33算法代码实例
2015/05/13 PHP
PHP实现的Redis多库选择功能单例类
2017/07/27 PHP
PHP基于imagick扩展实现合成图片的两种方法【附imagick扩展下载】
2017/11/14 PHP
php和nginx交互实例讲解
2019/09/24 PHP
再次更新!MSClass (Class Of Marquee Scroll通用不间断滚动JS封装类 Ver 1.6)
2007/02/05 Javascript
用js实现手把手教你月入万刀(转贴)
2007/11/07 Javascript
JavaScript 无符号右移运算符
2009/04/17 Javascript
JSQL SQLProxy 的 php 版本代码
2010/05/05 Javascript
基于Jquery的文字自动截取(提供源代码)
2011/08/09 Javascript
js bind 函数 使用闭包保存执行上下文
2011/12/26 Javascript
js操作checkbox遇到的问题解决
2013/06/29 Javascript
Jquery获取和修改img的src值的方法
2014/02/17 Javascript
Html5的placeholder属性(IE兼容)实现代码
2014/08/30 Javascript
jQuery中:eq()选择器用法实例
2014/12/29 Javascript
javascript闭包(Closure)用法实例简析
2015/11/30 Javascript
ES6学习教程之块级作用域详解
2017/10/09 Javascript
vuex直接赋值的三种方法总结
2018/09/16 Javascript
在微信小程序中保存网络图片
2019/02/12 Javascript
详解element-ui中form验证杂记
2019/03/04 Javascript
使用zrender.js绘制体温单效果
2019/10/31 Javascript
[02:33]DOTA2亚洲邀请赛趣味视频之吐真话筒
2018/03/31 DOTA
Python时区设置方法与pytz查询时区教程
2013/11/27 Python
利用Python3分析sitemap.xml并抓取导出全站链接详解
2017/07/04 Python
python查看模块,对象的函数方法
2018/10/16 Python
浅谈Python中的bs4基础
2018/10/21 Python
python推导式的使用方法实例
2021/02/28 Python
捷克汽车配件和工具销售网站:TorriaCars
2018/02/26 全球购物
入党申请人的自我鉴定
2013/12/01 职场文书
工作表现评语
2014/01/19 职场文书
教堂婚礼主持词
2014/03/14 职场文书
小学教师自我评价
2015/03/04 职场文书
经典爱情感言
2015/08/03 职场文书
2016大学生诚信考试承诺书
2016/03/25 职场文书
浅谈Node的内存泄露问题
2022/05/06 NodeJs
python自动获取微信公众号最新文章的实现代码
2022/07/15 Python