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字符串连接方式汇总
Aug 21 Python
Python fileinput模块使用实例
Jun 03 Python
python利用requests库进行接口测试的方法详解
Jul 06 Python
Python爬虫常用小技巧之设置代理IP
Sep 13 Python
Python 将Matrix、Dict保存到文件的方法
Oct 30 Python
实例讲解Python中浮点型的基本内容
Feb 11 Python
通过python实现弹窗广告拦截过程详解
Jul 10 Python
使用Python paramiko模块利用多线程实现ssh并发执行操作
Dec 05 Python
Django自定义YamlField实现过程解析
Nov 11 Python
python基础之模块的导入
Oct 24 Python
Python经常使用的一些内置函数
Apr 11 Python
使用Django框架创建项目
Jun 10 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面向对象编程快速入门
2006/12/14 PHP
将文件夹压缩成zip文件的php代码
2009/12/14 PHP
php.ini修改php上传文件大小限制的方法详解
2013/06/17 PHP
php中mysql连接方式PDO使用详解
2015/02/25 PHP
PHP定时执行任务实现方法详解(Timer)
2015/07/30 PHP
php通过smtp邮件验证登陆的方法
2016/05/11 PHP
laravel框架语言包拓展实现方法分析
2019/11/22 PHP
jquery.AutoComplete.js中文修正版(支持firefox)
2010/04/09 Javascript
jQuery 定时局部刷新(setInterval)
2010/11/19 Javascript
javascript自定义startWith()和endWith()的两种方法
2013/11/11 Javascript
Js保留小数点的4种效果实现代码分享
2014/04/12 Javascript
jQuery中:header选择器用法实例
2014/12/29 Javascript
jQuery实例—选项卡的简单实现(js源码和jQuery)
2016/06/14 Javascript
基于JavaScript实现的折半查找算法示例
2017/04/14 Javascript
基于jQuery对象和DOM对象和字符串之间的转化实例
2017/08/08 jQuery
Express系列之multer上传的使用
2017/10/27 Javascript
微信小程序使用wxParse解析html的方法教程
2018/07/06 Javascript
移动端(微信等使用vConsole调试console的方法
2019/03/05 Javascript
vue视图不更新情况详解
2019/05/16 Javascript
[05:29]2014DOTA2国际邀请赛 赛后专访:LGDNewbee顺利过关
2014/07/13 DOTA
python批量制作雷达图的实现方法
2016/07/26 Python
Python搭建FTP服务器的方法示例
2018/01/19 Python
python字符串string的内置方法实例详解
2018/05/14 Python
Python WEB应用部署的实现方法
2019/01/02 Python
手把手教你pycharm专业版安装破解教程(linux版)
2019/09/26 Python
Python3监控windows,linux系统的CPU、硬盘、内存使用率和各个端口的开启情况详细代码实例
2020/03/18 Python
Python 如何创建一个线程池
2020/07/28 Python
2020版Python学习路线图(附学习资料)
2020/09/15 Python
基于Python实现体育彩票选号器功能代码实例
2020/09/16 Python
经营管理策划方案
2014/05/22 职场文书
2014年销售人员工作总结
2014/11/27 职场文书
2014年副班长工作总结
2014/12/10 职场文书
幼儿园教师安全责任书
2015/05/08 职场文书
详解Mysql 函数调用优化
2021/04/07 MySQL
Vue3中toRef与toRefs的区别
2022/03/24 Vue.js
《总之就是很可爱》新作短篇动画《总之就是很可爱~制服~》将于2022年夏天播出
2022/04/07 日漫