Python之多张图片拼接

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zong596568821xp/article/details/83383433

参考:https://www.jianshu.com/p/9a4739420c9e

在做图像处理时,线阵相机采集保存的图片高度不够,需要将多张图片拼接在一起,原图片大小是2048×1024,需要将三张纵向拼接,形成大小为2048×3072的图片。话不多说,直接上代码

#!usr/bin/python
# -*- coding: utf-8 -*-

import os
from PIL import Image

high_size = 1024#高
width_size = 2048#宽

path = 'loulan_1/'  #原图片路径
savedpath = 'loulan_2/' #新图片保存路径
imghigh = 3 #三张合并在一起

imagefile = []
i = 0

filelist = os.listdir(path)
for item in filelist:
    i += 1
    if(i % 3 != 0):
        imagefile.append(Image.open(path + item))
    else:
        imagefile.append(Image.open(path + item))
        left = 0
        right = high_size
        target = Image.new('RGB',(width_size,high_size*imghigh))
        print('ready to save')
        for image in imagefile:
            target.paste(image,(0,left,width_size,right))
            left += high_size#从上往下拼接,左上角的纵坐标递增
            right += high_size#左下角的纵坐标也递增
            target.save(savedpath + item ,quality=100)
            print('save once')
        print('image of %s have been saved' %(item))
        #清空数组
        imagefile = [] 

猜你喜欢

转载自blog.csdn.net/zong596568821xp/article/details/83383433