Concatenate images using OpenCV in Python Last Updated : 03 Jan, 2023 Comments Improve Suggest changes Like Article Like Report To concatenate images vertically and horizontally with Python, cv2 library comes with two functions as: hconcat(): It is used as cv2.hconcat() to concatenate images horizontally. Here h means horizontal.vconcat(): It is used as cv2.vconcat() to concatenate images vertically. Here v means vertical.Implementation using hconcat() and vconcat() on Arrays A list of images as an n-dimensional array is passed in which images in the list are concatenated vertically or horizontally. Images with different sizes can be resized. The following ways to concatenate the images is explained through below the code as: Python3 # import cv2 library import cv2 # read the images img1 = cv2.imread('sea.jpg') img2 = cv2.imread('man.jpeg') Concatenate vertically: cv2.vconcat() is used to combine images of same width vertically. Python3 # vertically concatenates images # of same width im_v = cv2.vconcat([img1, img1]) # show the output image cv2.imshow('sea_image.jpg', im_v) Output: sea_image.jpg Concatenate horizontally: cv2.hconcat() is used to combine images of same height horizontally. Python3 # horizontally concatenates images # of same height im_h = cv2.hconcat([img2, img2]) # show the output image cv2.imshow('man_image.jpeg', im_h) Output: man_image.jpeg Concatenate images of different widths vertically: It is used to combine images of different widths. here shape[0] represents height and shape[1] represents width. Python3 # define a function for vertically # concatenating images of different # widths def vconcat_resize(img_list, interpolation = cv2.INTER_CUBIC): # take minimum width w_min = min(img.shape[1] for img in img_list) # resizing images im_list_resize = [cv2.resize(img, (w_min, int(img.shape[0] * w_min / img.shape[1])), interpolation = interpolation) for img in img_list] # return final image return cv2.vconcat(im_list_resize) # function calling img_v_resize = vconcat_resize([img1, img2, img1]) # show the output image cv2.imwrite('vconcat_resize.jpg', img_v_resize) Output: vconcat_resize.jpg Concatenate images of different heights horizontally: It is used to combine images of different heights. Python3 # define a function for horizontally # concatenating images of different # heights def hconcat_resize(img_list, interpolation = cv2.INTER_CUBIC): # take minimum hights h_min = min(img.shape[0] for img in img_list) # image resizing im_list_resize = [cv2.resize(img, (int(img.shape[1] * h_min / img.shape[0]), h_min), interpolation = interpolation) for img in img_list] # return final image return cv2.hconcat(im_list_resize) # function calling img_h_resize = hconcat_resize([img1, img2, img1]) # show the Output image cv2.imshow('hconcat_resize.jpg', img_h_resize) Output: hconcat_resize.jpg Concatenate images of the same size vertically and horizontally:  images can be combined using both cv2.hconcat() and cv2.vconcat() in tile form using a 2D list. Python3 # define a function for vertically # concatenating images of the # same size and horizontally def concat_vh(list_2d): # return final image return cv2.vconcat([cv2.hconcat(list_h) for list_h in list_2d]) # image resizing img1_s = cv2.resize(img1, dsize = (0,0), fx = 0.5, fy = 0.5) # function calling img_tile = concat_vh([[img1_s, img1_s, img1_s], [img1_s, img1_s, img1_s], [img1_s, img1_s, img1_s]]) # show the output image cv2.imshow('concat_vh.jpg', img_tile) Output: concat_vh.jpg Concatenate images of different sizes in vertical and horizontal tiles: Resizing and concatenating functions defined above are used to combine images of different sizes in vertical and horizontal tiles.  Python3 # define a function for concatenating # images of different sizes in # vertical and horizontal tiles def concat_tile_resize(list_2d, interpolation = cv2.INTER_CUBIC): # function calling for every # list of images img_list_v = [hconcat_resize(list_h, interpolation = cv2.INTER_CUBIC) for list_h in list_2d] # return final image return vconcat_resize(img_list_v, interpolation=cv2.INTER_CUBIC) # function calling im_tile_resize = concat_tile_resize([[img1], [img1, img2, img1, img2, img1], [img1, img2, img1]]) # show the image cv2.imshow('concat_tile_resize.jpg', im_tile_resize) Output: concat_tile_resize.jpg Comment More infoAdvertise with us Next Article Concatenate images using OpenCV in Python D divyanshu17bec1138 Follow Improve Article Tags : Python Computer Subject OpenCV Python-OpenCV Practice Tags : python Similar Reads Animate image using OpenCV in Python In this article, we will discuss how to animate an image using python's OpenCV module. Let's suppose we have one image. Using that single image we will animate it in such a way it will appear continuous array of the same image. This is useful in animating the background in certain games. For example 3 min read Color Identification in Images using Python - OpenCV An open-source library in Python, OpenCV is basically used for image and video processing. Not only supported by any system, such as Windows, Linux, Mac, etc. but also it can be run in any programming language like Python, C++, Java, etc. OpenCV also allows you to identify color in images. Donât you 3 min read How to Concatenate image using Pillow in Python ? Prerequisites: Python Pillow Concatenate image means joining of two images. We can merge any image whether it has different pixels, different image formats namely, 'jpeg', 'png', 'gif', 'tiff', etc. In python, we can join two images using the Python image library also known as the pillow library. In 3 min read Reading an image in OpenCV using Python Prerequisite: Basics of OpenCVIn this article, we'll try to open an image by using OpenCV (Open Source Computer Vision) library.  Following types of files are supported in OpenCV library:Windows bitmaps - *.bmp, *.dibJPEG files - *.jpeg, *.jpgPortable Network Graphics - *.png WebP - *.webp Sun raste 6 min read Addition and Blending of images using OpenCV in Python When we talk about images, we know its all about the matrix either binary image(0, 1), gray scale image(0-255) or RGB image(255 255 255). So additions of the image is adding the numbers of two matrices. In OpenCV, we have a command cv2.add() to add the images. Below is code for Addition of two image 2 min read Creating Hybrid Images Using OpenCV Library | Python Hybrid image, or "multi-scale image", is an exciting concept in computer vision. They are created by blending one image's high-frequency components with another's low-frequency components. The result is an image that appears as one when viewed from a distance but shows a different image when viewed 4 min read Check if the image is empty using Python - OpenCV Prerequisite: Basics of OpenCV OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on pictures or videos. It was originally developed by Intel but was later maintained by Willow Garage and is now maintained by Itseez. This library i 2 min read Add image to a live camera feed using OpenCV-Python In this article, we are going to learn how to insert an image in your live camera feed using OpenCV in Python. Stepwise ImplementationStep 1: Importing the libraries CV reads and stores all the images as a NumPy array. We need the NumPy library to manipulate the image and as expected we need the cv2 4 min read Python | Create video using multiple images using OpenCV Creating videos from multiple images is a great way for creating time-lapse videos. In this tutorial, weâll explore how to create a video from multiple images using Python and OpenCV. Creating a video from images involves combining multiple image frames, each captured at a specific moment in time, i 5 min read Convert OpenCV image to PIL image in Python OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrate 3 min read Like