MNA
MNA

Reputation: 287

Combine more than 1 openCV images and show them in CV2.Imshow() in OpenCV Python

I am having 3 to 4 images and I am trying to combine them all into one image (put into one window) and then show them through CV2.imshow() function. But the problem is that every solution to this problem is for exactly the same dimension images, which is not my case. My images are all of different dimensions. Kindly help me out how to solve this problem? I have four images with different dimension and want output like this

|||||||||||||||||||||||||||||||||

|| Image1 || Image2 ||

||||||||||||||||||||||||||||||||||

|| Image1 || Image2 ||

||||||||||||||||||||||||||||||||||

Currently, I have code like this for two images which work on only equally sized Images

im = cv2.imread('1.png')
img = cv2.imread('2.jpg')
both = np.hstack((im,im))
cv2.imshow('imgc',both)
cv2.waitKey(10000)

Upvotes: 1

Views: 9331

Answers (1)

bigbounty
bigbounty

Reputation: 17408

Use im.resize() function of opencv to resize the image and then do the combining task. Always use a reference dimension such as 1000 x 800 (you can change it)

import cv2

import numpy as np

list_of_img_paths = [path2,path3,path4]

im = cv2.imread(path1)

imstack = cv2.resize(im,(1000,800))

for path in list_of_img_paths:
    im = cv2.imread(path)
    im = cv2.resize(im,(1000,800))
    # hstack to join image horizontally  
    imstack = np.hstack((imstack,im))

cv2.imshow('stack',imstack)
cv2.waitKey(0)

Upvotes: 2

Related Questions