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

在Python中有没有办法将多个图像合并成一个图像?,python,opencv,image-processing,python-imaging-library,handwriting-recognition,Python,Opencv,Image Processing,Python Imaging Library,Handwriting Recognition,我想横向地将多个图像合并为一个图像。 我试图通过给定的代码合并图像,但它会生成白色图像? 对于合并图像,我尝试了PIL 我更喜欢使用OpenCV和Numpy组合。这意味着使用数组。 下面的代码只是将第一张图片作为起点-高度。任何附加的图像都将基于高度水平堆叠。这意味着,附加图像将根据蒙太奇高度调整大小,然后水平叠加到蒙太奇 工作代码 import cv2 import numpy as np image1 = cv2.imread("img1.jpg")[:,:,:3] image2

我想横向地将多个图像合并为一个图像。 我试图通过给定的代码合并图像,但它会生成白色图像? 对于合并图像,我尝试了PIL


我更喜欢使用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()
>>> m = Montage(image1)
>>> m.append(image2)
>>> m.show()
首先,用定义高度的第一个图像初始化类。因此,如果您想要不同的高度,请传递到已调整大小的类图像中。之后,您可以水平附加图像

用法

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()
>>> m = Montage(image1)
>>> m.append(image2)
>>> m.show()
您案例的结果:


但一般来说,它可以与完全不同的大小

图像1

图像2

蒙太奇

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()
>>> m = Montage(image1)
>>> m.append(image2)
>>> m.show()

更换您的线路:

images = map(Image.open,l)
与:


这一切都很好。

复制目标的可能副本也在垂直方向。如果这不是你的意思,请这样说(并解释你想如何合并图像)。我认为你是在新图像之外偏移图像。