The Wayback Machine - https://web.archive.org/web/20240627125132/https://www.geeksforgeeks.org/concatenate-images-using-opencv-in-python/
Open In App

Concatenate images using OpenCV in Python

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

To concatenate images vertically and horizontally with Python, cv2 library comes with two functions as:

  1. hconcat(): It is used as cv2.hconcat() to concatenate images horizontally. Here h means horizontal.
  2. 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



Similar Reads

Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
Prerequisite: Arithmetic Operations on Images | Set-1Bitwise operations are used in image manipulation and used for extracting essential parts in the image. In this article, Bitwise operations used are : ANDORXORNOT Also, Bitwise operations helps in image masking. Image creation can be enabled with the help of these operations. These operations can
4 min read
Python | Create video using multiple images using OpenCV
As we know OpenCV is a widely used library for image processing. It provides a wide sense of image processing. Let's see how to create video using multiple images using OpenCV. Install the following libraries: PIL cv2 Also, check the path before running the code otherwise you will be full with errors. How it works ? Using PIL library we are opening
3 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 images using OpenCV : # Python program for adding # ima
2 min read
Python | Denoising of colored images using opencv
Denoising of an image refers to the process of reconstruction of a signal from noisy images. Denoising is done to remove unwanted noise from image to analyze it in better form. It refers to one of the major pre-processing steps. There are four functions in opencv which is used for denoising of different images. Syntax: cv2.fastNlMeansDenoisingColor
1 min read
Python | Grayscaling of Images using OpenCV
Grayscaling is the process of converting an image from other color spaces e.g. RGB, CMYK, HSV, etc. to shades of gray. It varies between complete black and complete white. Importance of grayscaling Dimension reduction: For example, In RGB images there are three color channels and three dimensions while grayscale images are single-dimensional.Reduce
3 min read
How to Detect Shapes in Images in Python using OpenCV?
Prerequisites: OpenCV OpenCV is an open source library used mainly for processing images and videos to identify shapes, objects, text etc. It is mostly used with python. In this article we are going to see how to detect shapes in image. For this we need cv2.findContours() function of OpenCV, and also we are going to use cv2.drawContours() function
3 min read
How to subtract two images using Python-OpenCV ?
In this article, we are going to see how to subtract two images using OpenCV in Python. Now, before getting into the topic we shall discuss some of the use cases of Arithmetic operations. Arithmetic operations like addition and subtraction can help us to make images brighter or darker. Specifically, subtraction of two images has a lot of purposes i
3 min read
Drawing with Mouse on Images using Python-OpenCV
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. In this article, we will try to draw on images with the help of the mo
3 min read
Measure similarity between images using Python-OpenCV
Prerequisites: Python OpenCVSuppose we have two data images and a test image. Let's find out which data image is more similar to the test image using python and OpenCV library in Python.Let's first load the image and find out the histogram of images.Importing library import cv2 Importing image data image = cv2.imread('test.jpg') Converting to gray
2 min read
How to Display Multiple Images in One Window using OpenCV Python?
Prerequisites: Opencv In this article, we will show how to display Multiple Images In One window using OpenCV in Python. Approach Import moduleLoad the Multiple images using cv2.imread()Concatenate the images using concatenate(), with axis value provided as per orientation requirementDisplay all the images using cv2.imshow()Wait for keyboard button
1 min read
Practice Tags :
three90RightbarBannerImg