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
img1 = cv2.imread( 'sea.jpg' )
img2 = cv2.imread( 'man.jpeg' )
|
Concatenate vertically: cv2.vconcat() is used to combine images of same width vertically.
Python3
im_v = cv2.vconcat([img1, img1])
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
im_h = cv2.hconcat([img2, img2])
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
def vconcat_resize(img_list, interpolation
= cv2.INTER_CUBIC):
w_min = min (img.shape[ 1 ]
for img in img_list)
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 cv2.vconcat(im_list_resize)
img_v_resize = vconcat_resize([img1, img2, img1])
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
def hconcat_resize(img_list,
interpolation
= cv2.INTER_CUBIC):
h_min = min (img.shape[ 0 ]
for img in img_list)
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 cv2.hconcat(im_list_resize)
img_h_resize = hconcat_resize([img1, img2, img1])
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
def concat_vh(list_2d):
return cv2.vconcat([cv2.hconcat(list_h)
for list_h in list_2d])
img1_s = cv2.resize(img1, dsize = ( 0 , 0 ),
fx = 0.5 , fy = 0.5 )
img_tile = concat_vh([[img1_s, img1_s, img1_s],
[img1_s, img1_s, img1_s],
[img1_s, img1_s, img1_s]])
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
def concat_tile_resize(list_2d,
interpolation = cv2.INTER_CUBIC):
img_list_v = [hconcat_resize(list_h,
interpolation = cv2.INTER_CUBIC)
for list_h in list_2d]
return vconcat_resize(img_list_v, interpolation = cv2.INTER_CUBIC)
im_tile_resize = concat_tile_resize([[img1],
[img1, img2,
img1, img2, img1],
[img1, img2, img1]])
cv2.imshow( 'concat_tile_resize.jpg' , im_tile_resize)
|
Output:

concat_tile_resize.jpg
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
03 Jan, 2023
Like Article
Save Article