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算法之栈(stack)的实现
Aug 18 Python
用Python脚本来删除指定容量以上的文件的教程
May 04 Python
python遍历 truple list dictionary的几种方法总结
Sep 11 Python
python中利用xml.dom模块解析xml的方法教程
May 24 Python
Python实现简单文本字符串处理的方法
Jan 22 Python
python中dir()与__dict__属性的区别浅析
Dec 10 Python
详解如何在Apache中运行Python WSGI应用
Jan 02 Python
Python3.4学习笔记之 idle 清屏扩展插件用法分析
Mar 01 Python
Python图像处理库PIL的ImageFont模块使用介绍
Feb 26 Python
Scrapy基于scrapy_redis实现分布式爬虫部署的示例
Sep 29 Python
Python中lru_cache的使用和实现详解
Jan 25 Python
Python数据可视化之Seaborn的安装及使用
Apr 19 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&&mysql)六
2006/10/09 PHP
PHP5中虚函数的实现方法分享
2011/04/20 PHP
PHP中Session引起的脚本阻塞问题解决办法
2014/04/08 PHP
用 Composer构建自己的 PHP 框架之构建路由
2014/10/30 PHP
php微信开发之批量生成带参数的二维码
2016/06/26 PHP
JavaScript中使用构造器创建对象无需new的情况说明
2012/03/01 Javascript
文本框输入时 实现自动提示(像百度、google一样)
2012/04/05 Javascript
jquery实现效果比较好的table选中行颜色
2014/03/25 Javascript
jquery获取当前日期的方法
2015/01/14 Javascript
原生js实现类似弹窗抖动效果
2015/04/02 Javascript
深入讲解AngularJS中的自定义指令的使用
2015/06/18 Javascript
jQuery获取父元素及父节点的方法小结
2016/04/14 Javascript
jquery实现超简单的瀑布流布局【推荐】
2017/03/08 Javascript
node中Express 动态设置端口的方法
2017/08/04 Javascript
JavaScript实现兼容IE6的收起折叠与展开效果实例
2017/09/20 Javascript
详解NodeJs开发微信公众号
2018/05/25 NodeJs
React 组件中的 bind(this)示例代码
2018/09/16 Javascript
nodejs微信开发之自动回复的实现
2019/03/17 NodeJs
9个JavaScript日常开发小技巧
2020/10/06 Javascript
解决ant Design Search无法输入内容的问题
2020/10/29 Javascript
vue 实现基础组件的自动化全局注册
2020/12/25 Vue.js
[38:44]DOTA2上海特级锦标赛A组小组赛#2 Secret VS CDEC第二局
2016/02/25 DOTA
[02:02]特效爆炸!DOTA2珍宝之瓶待你开启
2018/08/21 DOTA
python实现可变变量名方法详解
2019/07/01 Python
django中forms组件的使用与注意
2019/07/08 Python
python画蝴蝶曲线图的实例
2019/11/21 Python
Django中密码的加密、验密、解密操作
2019/12/19 Python
Python3 用什么IDE开发工具比较好
2020/11/28 Python
canvas实现按住鼠标移动绘制出轨迹的示例代码
2018/02/05 HTML / CSS
Crucial英睿达法国官网:内存条及SSD固态硬盘升级
2018/07/13 全球购物
华为智利官方商店:Huawei Chile
2020/05/09 全球购物
PHP开发工程师面试问题集锦
2012/11/01 面试题
元旦寄语大全
2014/04/10 职场文书
2014年小学校长工作总结
2014/12/08 职场文书
用Python创建简易网站图文教程
2021/06/11 Python
Python 避免字典和元组的多重嵌套问题
2022/07/15 Python