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数据结构之Array用法实例
Oct 09 Python
Python中Random和Math模块学习笔记
May 18 Python
python类和继承用法实例
Jul 07 Python
python 自动化将markdown文件转成html文件的方法
Sep 23 Python
Python Sqlite3以字典形式返回查询结果的实现方法
Oct 03 Python
Python进阶篇之字典操作总结
Nov 16 Python
selenium跳过webdriver检测并模拟登录淘宝
Jun 12 Python
python中利用matplotlib读取灰度图的例子
Dec 07 Python
Python reversed函数及使用方法解析
Mar 17 Python
python OpenCV学习笔记
Mar 31 Python
Python机器学习之逻辑回归
May 11 Python
python Django框架快速入门教程(后台管理)
Jul 21 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中addslashes函数与sql防注入
2014/11/17 PHP
PHP使用数组依次替换字符串中匹配项
2016/01/08 PHP
php flush无效,IIS7下php实时输出的方法
2016/08/25 PHP
PHP文件操作详解
2016/12/30 PHP
Laravel框架基于ajax和layer.js实现无刷新删除功能示例
2019/01/17 PHP
PHP封装的mysqli数据库操作类示例
2019/02/16 PHP
PHP实现的只保留字符串首尾字符功能示例【隐藏部分字符串】
2019/03/11 PHP
Prototype Template对象 学习
2009/07/19 Javascript
js动态调用css属性的小规律及实例说明
2013/12/28 Javascript
nodejs实现获取某宝商品分类
2015/05/28 NodeJs
jQuery实现在最后一个元素之前插入新元素的方法
2015/07/18 Javascript
JS函数arguments数组获得实际传参数个数的实现方法
2016/05/28 Javascript
微信小程序 点击控件后选中其它反选实例详解
2017/02/21 Javascript
bootstrap fileinput 插件使用项目总结(经验)
2017/02/22 Javascript
VUE 更好的 ajax 上传处理 axios.js实现代码
2017/05/10 Javascript
原生js简单实现放大镜特效
2017/05/16 Javascript
angular-ngSanitize模块-$sanitize服务详解
2017/06/13 Javascript
ReactNative 之FlatList使用及踩坑封装总结
2017/11/29 Javascript
解决vue 中 echart 在子组件中只显示一次的问题
2018/08/07 Javascript
解决Vue打包后访问图片/图标不显示的问题
2019/07/25 Javascript
layui table设置某一行的字体颜色方法
2019/09/05 Javascript
js滚轮事件 js自定义滚动条的实现
2020/01/18 Javascript
es6中let和const的使用方法详解
2020/02/24 Javascript
vue 动态生成拓扑图的示例
2021/01/03 Vue.js
[01:00:10]完美世界DOTA2联赛PWL S2 FTD vs Inki 第二场 11.21
2020/11/24 DOTA
PyQt5每天必学之带有标签的复选框
2018/04/19 Python
在Pycharm中设置默认自动换行的方法
2019/01/16 Python
python,Django实现的淘宝客登录功能示例
2019/06/12 Python
python算法题 链表反转详解
2019/07/02 Python
django admin 根据choice字段选择的不同来显示不同的页面方式
2020/05/13 Python
python opencv图像处理(素描、怀旧、光照、流年、滤镜 原理及实现)
2020/12/10 Python
使用bandit对目标python代码进行安全函数扫描的案例分析
2021/01/27 Python
以太网Ethernet IEEE802.3
2013/08/05 面试题
遇到的Mysql的面试题
2014/06/29 面试题
员工评语大全
2014/01/19 职场文书
使用nginx配置访问wgcloud的方法
2021/06/26 Servers