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的Django框架下使用django-tagging的教程
May 30 Python
常用python编程模板汇总
Feb 12 Python
浅谈Python里面小数点精度的控制
Jul 16 Python
python 3.7.0 下pillow安装方法
Aug 27 Python
详解Python字典的操作
Mar 04 Python
Python3.5基础之NumPy模块的使用图文与实例详解
Apr 24 Python
anaconda中更改python版本的方法步骤
Jul 14 Python
Python 获取windows桌面路径的5种方法小结
Jul 15 Python
python为QT程序添加图标的方法详解
Mar 09 Python
浅谈Python中re.match()和re.search()的使用及区别
Apr 14 Python
Python基于gevent实现高并发代码实例
May 15 Python
Python 如何实现数据库表结构同步
Sep 29 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怎样用正则抓取页面中的网址
2016/08/09 PHP
PHP parse_ini_file函数的应用与扩展操作示例
2019/01/07 PHP
[转]JS宝典学习笔记
2007/02/07 Javascript
用js实现手把手教你月入万刀(转贴)
2007/11/07 Javascript
jQuery中的常用事件总结
2009/12/27 Javascript
多个js与css文件的合并方法详细说明
2012/12/26 Javascript
js绑定事件this指向发生改变的问题解决方法
2013/04/23 Javascript
浅谈document.write()输出样式
2015/05/07 Javascript
JavaScript实现鼠标点击后层展开效果的方法
2015/05/13 Javascript
js图片翻书效果代码分享
2015/08/20 Javascript
JavaScript学习小结(一)——JavaScript入门基础
2015/09/02 Javascript
Javascript基于AJAX回调函数传递参数实例分析
2015/12/15 Javascript
使用CSS+JavaScript或纯js实现半透明遮罩效果的实例分享
2016/05/09 Javascript
jQuery得到多个值只能用取Class ,不能用取ID的方法
2016/12/04 Javascript
理解nodejs的stream和pipe机制的原理和实现
2017/08/12 NodeJs
使用jQuery实现购物车结算功能
2017/08/15 jQuery
Vue组件选项props实例详解
2017/08/18 Javascript
ES6中新增的Object.assign()方法详解
2017/09/22 Javascript
jQuery中过滤器的基本用法示例
2017/10/11 jQuery
详解create-react-app 2.0版本如何启用装饰器语法
2018/10/23 Javascript
JavaScript Dom实现轮播图原理和实例
2021/02/19 Javascript
[02:15]2014DOTA2国际邀请赛 赛后退役选手回顾
2014/08/01 DOTA
Python中__name__的使用实例
2015/04/14 Python
python 字符串转列表 list 出现\ufeff的解决方法
2017/06/22 Python
Python机器学习算法之k均值聚类(k-means)
2018/02/23 Python
tensorflow实现简单的卷积神经网络
2018/05/24 Python
python画微信表情符的实例代码
2019/10/09 Python
Python 使用 PyQt5 开发的关机小工具分享
2020/07/16 Python
马来西亚最大的电器网站:Senheng
2017/10/13 全球购物
印度在线杂货店:bigbasket
2018/08/23 全球购物
一份软件工程师的面试试题
2016/02/01 面试题
写好求职应聘自荐信的三部曲
2013/09/21 职场文书
求职个人评价范文
2014/04/09 职场文书
三八红旗集体先进事迹材料
2014/05/22 职场文书
观看《筑梦中国》纪录片心得体会
2016/01/18 职场文书
尝试使用Python爬取城市租房信息
2022/04/12 Python