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调用短信猫控件实现发短信功能实例
Jul 04 Python
win7上python2.7连接mysql数据库的方法
Jan 14 Python
一个基于flask的web应用诞生 使用模板引擎和表单插件(2)
Apr 11 Python
numpy.std() 计算矩阵标准差的方法
Jul 11 Python
详解从Django Rest Framework响应中删除空字段
Jan 11 Python
python调用c++ ctype list传数组或者返回数组的方法
Feb 13 Python
Python中注释(多行注释和单行注释)的用法实例
Aug 28 Python
python 如何将数据写入本地txt文本文件的实现方法
Sep 11 Python
Python脚本如何在bilibili中查找弹幕发送者
Jun 04 Python
属性与 @property 方法让你的python更高效
Sep 21 Python
Pycharm中使用git进行合作开发的教程详解
Nov 17 Python
python爬虫中url管理器去重操作实例
Nov 30 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随机生成数字字母组合的方法
2015/03/18 PHP
解析WordPress中函数钩子hook的作用及基本用法
2015/12/22 PHP
thinkphp中U方法按路由规则生成url的方法
2018/03/12 PHP
PHP与Web页面的交互示例详解一
2020/08/04 PHP
事件绑定之小测试  onclick && addEventListener
2011/07/31 Javascript
模拟用户点击弹出新页面不会被浏览器拦截
2014/04/08 Javascript
深入探讨JavaScript、JQuery屏蔽网页鼠标右键菜单及禁止选择复制
2014/06/10 Javascript
文本框倒叙输入让输入框的焦点始终在最开始的位置
2014/09/01 Javascript
深入理解javascript构造函数和原型对象
2014/09/23 Javascript
jquery控制背景音乐开关与自动播放提示音的方法
2015/02/06 Javascript
jQuery实现简单的间隔向上滚动效果
2015/03/09 Javascript
jQuery弹出层插件Lightbox_me使用指南
2015/04/21 Javascript
jQuery实现简单倒计时功能的方法
2016/07/04 Javascript
BootStrap中Table分页插件使用详解
2016/10/09 Javascript
JSON对象 详解及实例代码
2016/10/18 Javascript
微信JS SDK接入的几点注意事项(必看篇)
2017/06/23 Javascript
angular使用bootstrap方法手动启动的实例代码
2017/07/18 Javascript
微信小程序如何获取用户信息
2018/01/26 Javascript
微信小程序之事件交互操作实例分析
2018/12/03 Javascript
利用vue-i18n实现多语言切换效果的方法
2019/06/19 Javascript
JavaScript实现省市区三级联动
2020/02/13 Javascript
使用Vue实现简单计算器
2020/02/25 Javascript
js实现翻牌小游戏
2020/07/31 Javascript
[48:28]完美世界DOTA2联赛循环赛FTD vs Magma第二场 10月30日
2020/10/31 DOTA
Python3.6使用tesseract-ocr的正确方法
2018/10/17 Python
pytorch 预训练层的使用方法
2019/08/20 Python
Python使用psutil获取进程信息的例子
2019/12/17 Python
关于Python3爬虫利器Appium的安装步骤
2020/07/29 Python
鼠标滚轮事件和Mac触控板双指事件
2019/12/23 HTML / CSS
德国电子商城:ComputerUniverse
2017/04/21 全球购物
2014年高中教师工作总结
2014/12/19 职场文书
暑期社会实践个人总结
2015/03/06 职场文书
熟背这些句子,让您的英语口语突飞猛进(135句)
2019/09/06 职场文书
如何用六步教会你使用python爬虫爬取数据
2022/04/06 Python
开发者首先否认《遗弃》被取消的传言
2022/04/11 其他游戏
mysql中如何用命令创建联合唯一索引
2022/04/20 MySQL