Concatenate images using OpenCV in Python - GeeksforGeeks

28 Jul, 2020
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.
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.
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 libraryimport cv2  # read the imagesimg1 = 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 imagecv2.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 imagecv2.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 callingimg_v_resize = vconcat_resize([img1, img2, img1])  # show the output imagecv2.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 callingimg_h_resize = hconcat_resize([img1, img2, img1])  # show the Output imagecv2.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 horizontallydef concat_vh(list_2d):          # return final image    return cv2.vconcat([cv2.hconcat(list_h)                         for list_h in list_2d])# image resizingimg1_s = cv2.resize(img1, dsize = (0,0),                    fx = 0.5, fy = 0.5)  # function callingimg_tile = concat_vh([[img1_s, img1_s, img1_s],                      [img1_s, img1_s, img1_s],                      [img1_s, img1_s, img1_s]])# show the output imagecv2.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 tilesdef 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 callingim_tile_resize = concat_tile_resize([[img1],                                     [img1, img2,                                      img1, img2, img1],                                     [img1, img2, img1]])# show the imagecv2.imshow('concat_tile_resize.jpg', im_tile_resize)
Output:
concat_tile_resize.jpg
Python-OpenCV
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists