Use cv2.vconcat(), im_v = cv2.vconcat([im1, im1]) cv2.imwrite('data/dst/opencv_vconcat.jpg', im_v) 0 to concatenate (combine) images vertically and horizontally with Python, OpenCV. im_v = cv2.vconcat([im1, im1]) cv2.imwrite('data/dst/opencv_vconcat.jpg', im_v) 1 means vertical and im_v = cv2.vconcat([im1, im1]) cv2.imwrite('data/dst/opencv_vconcat.jpg', im_v) 2 means horizontal.
Pass a list of images (im_v = cv2.vconcat([im1, im1]) cv2.imwrite('data/dst/opencv_vconcat.jpg', im_v) 3), an image (im_v = cv2.vconcat([im1, im1]) cv2.imwrite('data/dst/opencv_vconcat.jpg', im_v) 3) in which the images in the list are vertically or horizontally concatenated is returned. Images with different sizes need to be resized beforehand. An error is raised if the width or height is not aligned.
This article describes the following contents with sample codes.
- Concatenate vertically: cv2.vconcat()
- Concatenate horizontally: im_v = cv2.vconcat([im1, im1]) cv2.imwrite('data/dst/opencv_vconcat.jpg', im_v) 0
- Concatenate vertically and horizontally (like tiles)
Read two images as an example.
import cv2 import numpy as np im1 = cv2.imread('data/src/lena.jpg') im2 = cv2.imread('data/src/rocket.jpg')
source:
im_v = cv2.vconcat([im1, im1]) cv2.imwrite('data/dst/opencv_vconcat.jpg', im_v) 7 is convenient when arranging the same image repeatedly.
- NumPy: Arrange ndarray in tiles with np.tile()
For more information on image concatenation using Pillow and scikit-image, see the following articles. For images of the same size, scikit-image is easy to use. You can also add a border between the images.
- Concatenate images with Python, Pillow
- Create a montage of images with Python, scikit-image (skimage.util.montage)
Sponsored Link
Concatenate vertically: cv2.vconcat()
Concatenate images of the same width vertically
When concatenating images of the same width vertically, cv2.vconcat() can be used as it is.
im_v = cv2.vconcat([im1, im1]) cv2.imwrite('data/dst/opencv_vconcat.jpg', im_v)
source:
Repeat the same image vertically
You can use im_v_np = np.tile(im1, (2, 1, 1)) cv2.imwrite('data/dst/opencv_vconcat_np.jpg', im_v_np) 0 as well if you arrange the same image repeatedly.
im_v_np = np.tile(im1, (2, 1, 1)) cv2.imwrite('data/dst/opencv_vconcat_np.jpg', im_v_np)
source:
Concatenate images of different widths vertically
It is useful to define a function to combine images of different widths. Here, the bigger one is resized.
im_v_np = np.tile(im1, (2, 1, 1)) cv2.imwrite('data/dst/opencv_vconcat_np.jpg', im_v_np) 1 represents height and im_v_np = np.tile(im1, (2, 1, 1)) cv2.imwrite('data/dst/opencv_vconcat_np.jpg', im_v_np) 2 represents width.
- Get image size (width, height) with Python, OpenCV, Pillow (PIL)
def vconcat_resize_min(im_list, interpolation=cv2.INTER_CUBIC): w_min = min(im.shape[1] for im in im_list) im_list_resize = [cv2.resize(im, (w_min, int(im.shape[0] * w_min / im.shape[1])), interpolation=interpolation) for im in im_list] return cv2.vconcat(im_list_resize) im_v_resize = vconcat_resize_min([im1, im2, im1]) cv2.imwrite('data/dst/opencv_vconcat_resize.jpg', im_v_resize)
source:
List comprehensions are used to create a list of resized images.
- List comprehensions in Python
Concatenate horizontally: im_v = cv2.vconcat([im1, im1]) cv2.imwrite('data/dst/opencv_vconcat.jpg', im_v) 0
The horizontal case is basically the same concept as the vertical case.
Concatenate images of the same height horizontally
When concatenating images of the same height horizontally, im_v = cv2.vconcat([im1, im1]) cv2.imwrite('data/dst/opencv_vconcat.jpg', im_v) 0 can be used as it is.
im_h = cv2.hconcat([im1, im1]) cv2.imwrite('data/dst/opencv_hconcat.jpg', im_h)
source:
Repeat the same image horizontally
You can use im_v_np = np.tile(im1, (2, 1, 1)) cv2.imwrite('data/dst/opencv_vconcat_np.jpg', im_v_np) 0 as well if you arrange the same image repeatedly.
im_h_np = np.tile(im1, (1, 2, 1)) cv2.imwrite('data/dst/opencv_hconcat_np.jpg', im_h_np)
source:
Concatenate images of different heights horizontally
As with vertical concatenation, it is useful to define a function to combine images of different sizes. Here, the bigger one is resized.
def hconcat_resize_min(im_list, interpolation=cv2.INTER_CUBIC): h_min = min(im.shape[0] for im in im_list) im_list_resize = [cv2.resize(im, (int(im.shape[1] * h_min / im.shape[0]), h_min), interpolation=interpolation) for im in im_list] return cv2.hconcat(im_list_resize) im_h_resize = hconcat_resize_min([im1, im2, im1]) cv2.imwrite('data/dst/opencv_hconcat_resize.jpg', im_h_resize)
source:
Sponsored Link
Concatenate vertically and horizontally (like tiles)
Concatenate images of the same size vertically and horizontally
Using cv2.vconcat() and im_v = cv2.vconcat([im1, im1]) cv2.imwrite('data/dst/opencv_vconcat.jpg', im_v) 0, images can be concatenated vertically and horizontally in tile form.
A function that concatenates images of the same size with a 2D list (array) can be defined as follows:
def concat_tile(im_list_2d): return cv2.vconcat([cv2.hconcat(im_list_h) for im_list_h in im_list_2d]) im1_s = cv2.resize(im1, dsize=(0, 0), fx=0.5, fy=0.5) im_tile = concat_tile([[im1_s, im1_s, im1_s, im1_s], [im1_s, im1_s, im1_s, im1_s], [im1_s, im1_s, im1_s, im1_s]]) cv2.imwrite('data/dst/opencv_concat_tile.jpg', im_tile)
source:
The example uses the same image for simplicity, but it is useful when comparing the results of changing the image processing coefficients.
Concatenate same images vertically and horizontally
When arranging the same image repeatedly, im_v_np = np.tile(im1, (2, 1, 1)) cv2.imwrite('data/dst/opencv_vconcat_np.jpg', im_v_np) 0 can be used as before.
When concatenating color images (3D im_v = cv2.vconcat([im1, im1]) cv2.imwrite('data/dst/opencv_vconcat.jpg', im_v) 3), set the second parameter (def vconcat_resize_min(im_list, interpolation=cv2.INTER_CUBIC): w_min = min(im.shape[1] for im in im_list) im_list_resize = [cv2.resize(im, (w_min, int(im.shape[0] * w_min / im.shape[1])), interpolation=interpolation) for im in im_list] return cv2.vconcat(im_list_resize) im_v_resize = vconcat_resize_min([im1, im2, im1]) cv2.imwrite('data/dst/opencv_vconcat_resize.jpg', im_v_resize) 0) to def vconcat_resize_min(im_list, interpolation=cv2.INTER_CUBIC): w_min = min(im.shape[1] for im in im_list) im_list_resize = [cv2.resize(im, (w_min, int(im.shape[0] * w_min / im.shape[1])), interpolation=interpolation) for im in im_list] return cv2.vconcat(im_list_resize) im_v_resize = vconcat_resize_min([im1, im2, im1]) cv2.imwrite('data/dst/opencv_vconcat_resize.jpg', im_v_resize) 1. For grayscale images (2D im_v = cv2.vconcat([im1, im1]) cv2.imwrite('data/dst/opencv_vconcat.jpg', im_v) 3), set def vconcat_resize_min(im_list, interpolation=cv2.INTER_CUBIC): w_min = min(im.shape[1] for im in im_list) im_list_resize = [cv2.resize(im, (w_min, int(im.shape[0] * w_min / im.shape[1])), interpolation=interpolation) for im in im_list] return cv2.vconcat(im_list_resize) im_v_resize = vconcat_resize_min([im1, im2, im1]) cv2.imwrite('data/dst/opencv_vconcat_resize.jpg', im_v_resize) 0 to def vconcat_resize_min(im_list, interpolation=cv2.INTER_CUBIC): w_min = min(im.shape[1] for im in im_list) im_list_resize = [cv2.resize(im, (w_min, int(im.shape[0] * w_min / im.shape[1])), interpolation=interpolation) for im in im_list] return cv2.vconcat(im_list_resize) im_v_resize = vconcat_resize_min([im1, im2, im1]) cv2.imwrite('data/dst/opencv_vconcat_resize.jpg', im_v_resize) 4.
See the following article for more information on im_v_np = np.tile(im1, (2, 1, 1)) cv2.imwrite('data/dst/opencv_vconcat_np.jpg', im_v_np) 0.
- NumPy: Arrange ndarray in tiles with np.tile()
Concatenate images of different sizes in vertical and horizontal tiles
When concatenating images of different sizes in vertical and horizontal tiles, use the resizing and concatenating function defined above.