python使用PIL实现多张图片垂直合并

本文实例为大家分享了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'])

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • python实现多张图片拼接成大图

    本文实例为大家分享了python实现多张图片拼接成大图的具体代码,供大家参考,具体内容如下 上次爬取了马蜂窝的游记图片,并解决了PIL模块的导入问题,现在直奔主题吧: import PIL.Image as Image import os IMAGES_PATH = 'D:\Mafengwo\photo\五月坦桑的暖风,非洲原野的呼唤\\' # 图片集地址 IMAGES_FORMAT = ['.jpg', '.JPG'] # 图片格式 IMAGE_SIZE = 256 # 每张小图片的大小 IM

  • python+opencv 读取文件夹下的所有图像并批量保存ROI的方法

    如下所示: import cv2 import os import numpy as np root_path = "I:/Images/2017_08_03/" dir = root_path+"images"+"/" count = 0 for root,dir,files in os.walk(dir): for file in files: srcImg = cv2.imread(root_path+"images"+

  • Python饼状图的绘制实例

    import numpy as np import matplotlib.pyplot as plt labels = 'A', 'B', 'C', 'D' fracs = [15, 30.55, 44.44, 10] explode = [0, 0, 0, 0] # 0.1 凸出这部分, plt.axes(aspect=1) # set this , Figure is round, otherwise it is an ellipse # autopct ,show percet plt.p

  • Python使用matplotlib的pie函数绘制饼状图功能示例

    本文实例讲述了Python使用matplotlib的pie函数绘制饼状图功能.分享给大家供大家参考,具体如下: matplotlib具体安装方法可参考前面一篇http://www.jb51.net/article/51812.htm,具体使用代码如下: #coding=utf8 import matplotlib as mpl import numpy as np import matplotlib.pyplot as plt ''''' matplotlib.pyplot.pie函数:画一个饼

  • python实现图片彩色转化为素描

    本文实例为大家分享了Python将图片彩色转化为素描的具体代码,供大家参考,具体内容如下 第一种: from PIL import Image, ImageFilter, ImageOps img = Image.open('E:\\picture\\1.png') def dodge(a, b, alpha): return min(int(a*255/(256-b*alpha)), 255) def draw(img, blur=25, alpha=1.0): img1 = img.conv

  • python将处理好的图像保存到指定目录下的方法

    原始图像绝对路径的图像名存储在一个txt文件中,下面的程序实现的功能是按照txt文件的顺序,依次将图片读取然后进行处理,最后将处理之后的图像保存在指定的路径下: # Read in the image to be detected # 原始图像均保存在binaries.txt文件中,将包含绝对目录的图像名提取出来并写到txt文件的程序见上一篇博客 f = open("/home/shenruixue/image_test/binaries.txt") line = f.readline

  • Python数据可视化教程之Matplotlib实现各种图表实例

    前言 数据分析就是将数据以各种图表的形式展现给领导,供领导做决策用,因此熟练掌握饼图.柱状图.线图等图表制作是一个数据分析师必备的技能.Python有两个比较出色的图表制作框架,分别是Matplotlib和Pyechart.本文主要讲述使用Matplotlib制作各种数据图表. Matplotlib是最流行的用于绘制2D数据图表的Python库,能够在各种平台上使用,可以绘制散点图.柱状图.饼图等. 1.柱状图 是一种以长方形或长方体的高度为变量的表达图形的统计报告图,由一系列高度不等的纵向条纹

  • python实现彩色图转换成灰度图

    本文实例为大家分享了python实现彩色图转换成灰度图的具体代码,供大家参考,具体内容如下 from PIL import Image import os # 图像组成:红绿蓝 (RGB)三原色组成 亮度(255,255,255) image = "Annie1.jpg" img = Image.open(image) img_all = "素描" + image new = Image.new("L", img.size, 255) width

  • python使用matplotlib画饼状图

    本文实例为大家分享了python使用matplotlib画饼状图的具体代码,供大家参考,具体内容如下 代码与详细注释 from matplotlib import pyplot as plt #调节图形大小,宽,高 plt.figure(figsize=(6,9)) #定义饼状图的标签,标签是列表 labels = [u'第一部分',u'第二部分',u'第三部分'] #每个标签占多大,会自动去算百分比 sizes = [60,30,10] colors = ['red','yellowgreen

  • Python数据可视化之画图

    安装数据可视化模块matplotlib:pip install matplotlib 导入matplotlib模块下的pyplot 1 折线图 from matplotlib import pyplot #横坐标 year=[2010,2012,2014,2016] #纵坐标 perple=[20,40,60,100] #生成折线图:函数polt pyplot.plot(year,perple) #设置横坐标说明 pyplot.xlabel('year') #设置纵坐标说明 pyplot.yla

随机推荐