Skip to content
Related Articles

Related Articles

Improve Article

Concatenate images using OpenCV in Python

  • Last Updated : 28 Jul, 2020

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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :