有没有办法在Python中将多个图像合并为一个图像?

时间:2019-03-17 14:13:30

标签: python opencv image-processing python-imaging-library handwriting-recognition

我想将多个图像水平合并为一个图像。 我试图通过给定的代码合并图像,但它给出了白色图像? 对于合并图像,我尝试使用PIL。

Input1

Input2

Input3

output Image

assertThrows

2 个答案:

答案 0 :(得分:0)

我更喜欢使用OpenCV&Numpy组合。这意味着使用数组。 下面的代码仅以第一张图片为起点-高度。您要附加的任何图像将根据高度水平堆叠。这意味着,附加的图像将根据蒙太奇高度调整大小,然后水平堆叠到蒙太奇。

工作代码

import cv2
import numpy as np

image1 = cv2.imread("img1.jpg")[:,:,:3]
image2 = cv2.imread("img2.jpg")[:,:,:3]

class Montage(object):
    def __init__(self,initial_image):
        self.montage = initial_image
        self.x,self.y = self.montage.shape[:2]

    def append(self,image):
        image = image[:,:,:3]
        x,y = image.shape[0:2]
        new_image = cv2.resize(image,(int(y*float(self.x)/x),self.x))
        self.montage = np.hstack((self.montage,new_image))
    def show(self):
        cv2.imshow('montage',self.montage)
        cv2.waitKey()
        cv2.destroyAllWindows()

首先,使用将定义HEIGHT的第一张图片初始化类。因此,如果您想要不同的高度,请传递给类调整大小的图像。之后,您可以水平添加图像

用法

>>> m = Montage(image1)
>>> m.append(image2)
>>> m.show()

您的案例结果: enter image description here


但是通常它可以使用完全不同的大小

图片1

enter image description here

图片2

enter image description here

蒙太奇

enter image description here

答案 1 :(得分:0)

替换您的行:

images = map(Image.open,l)

具有:

images = [ Image.open(im) for im in l]

一切正常。