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处理圆角图片、圆形图片的例子
Apr 25 Python
10种检测Python程序运行时间、CPU和内存占用的方法
Apr 01 Python
在Gnumeric下使用Python脚本操作表格的教程
Apr 14 Python
django中模板的html自动转意方法
May 27 Python
Django+Ajax+jQuery实现网页动态更新的实例
May 28 Python
python脚本开机自启的实现方法
Jun 28 Python
Python Matplotlib 基于networkx画关系网络图
Jul 10 Python
浅谈Python type的使用
Nov 19 Python
Python如何实现强制数据类型转换
Nov 22 Python
Python GUI库PyQt5样式QSS子控件介绍
Feb 25 Python
python读取xml文件方法解析
Aug 04 Python
python 将Excel转Word的示例
Mar 02 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
实用函数2
2007/11/08 PHP
php设置编码格式的方法
2013/03/05 PHP
完美解决thinkphp验证码出错无法显示的方法
2014/12/09 PHP
php打包网站并在线压缩为zip
2016/02/13 PHP
php 使用ActiveMQ发送消息,与处理消息操作示例
2020/02/23 PHP
关于文本限制字数的js代码
2007/04/02 Javascript
JQuery 遮罩层实现(mask)实现代码
2010/01/09 Javascript
js 全兼容可高亮二级缓冲折叠菜单
2010/06/04 Javascript
网页加载时页面显示进度条加载完成之后显示网页内容
2012/12/23 Javascript
JS实现随机数生成算法示例代码
2013/08/08 Javascript
JS控制一个DIV层在指定时间内消失的方法
2014/02/17 Javascript
JS实现闪动的title消息提醒效果
2014/06/20 Javascript
javascript实现倒计时N秒后网页自动跳转代码
2014/12/11 Javascript
jquery插件jquery.LightBox.js实现点击放大图片并左右点击切换效果(附demo源码下载)
2016/02/25 Javascript
EasyUi中的Combogrid 实现分页和动态搜索远程数据
2016/04/01 Javascript
浅析JS中的 map, filter, some, every, forEach, for in, for of 用法总结
2017/03/29 Javascript
微信小程序 action-sheet 反馈上拉菜单简单实例
2017/05/11 Javascript
JS实现的JSON序列化操作简单示例
2018/07/02 Javascript
Element-UI踩坑之Pagination组件的使用
2018/10/29 Javascript
[01:11]steam端dota2实名认证操作流程视频
2021/03/11 DOTA
python基础教程之数字处理(math)模块详解
2014/03/25 Python
python实现自动重启本程序的方法
2015/07/09 Python
python实现windows倒计时锁屏功能
2019/07/30 Python
Xadmin+rules实现多选行权限方式(级联效果)
2020/04/07 Python
Python实现转换图片背景颜色代码
2020/04/30 Python
python安装第三方库如xlrd的方法
2020/10/31 Python
Css3新特性应用之形状总结
2016/12/08 HTML / CSS
《王二小》教学反思
2014/02/27 职场文书
立志成才演讲稿
2014/09/04 职场文书
大三学生学年自我鉴定
2014/09/12 职场文书
逃课检讨书怎么写
2015/01/01 职场文书
高中教师个人工作总结
2015/02/10 职场文书
教师节老师寄语
2015/05/28 职场文书
Nginx开启Brotli压缩算法实现过程详解
2021/03/31 Servers
解决vue $http的get和post请求跨域问题
2021/06/07 Vue.js
Redis模仿手机验证码发送的实现示例
2021/11/02 Redis