How do I make rectangular image squared using OpenCV and Python?

Viewed 4608

I have all sorts of images of rectangular shape. I need to modify them to uniform square shape (different size ok).

For that I have to layer it on top of larger squared shape. Background is black.

I figured it to the point when I need to layer 2 images:

import cv2
import numpy as np
if 1:
        img = cv2.imread(in_img)
        #get size
        height, width, channels = img.shape
        print (in_img,height, width, channels)
        # Create a black image
        x = height if height > width else width
        y = height if height > width else width
        square= np.zeros((x,y,3), np.uint8)
        cv2.imshow("original", img)
        cv2.imshow("black square", square)
        cv2.waitKey(0)

How do I stack them on top of each other so original image is centered vertically and horizontally on top of black shape?

2 Answers
Related