import cv2 import numpy as np import matplotlib.pyplot as plt def show_image(img): """Display an image in PyCharm.""" img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert BGR to RGB (if using OpenCV) plt.imshow(img_rgb) plt.axis("off") # Hide axes plt.show() def resize_and_pad(image, target_shape): h, w = image.shape[:2] target_h, target_w = target_shape scale = min(target_w / w, target_h / h) new_w, new_h = int(w * scale), int(h * scale) resized = cv2.resize(image, (new_w, new_h), interpolation=cv2.INTER_AREA) canvas = np.zeros((target_h, target_w, 3), dtype=np.uint8) top = (target_h - new_h) // 2 left = (target_w - new_w) // 2 canvas[top:top + new_h, left:left + new_w] = resized return canvas def create_instance(img1: np.ndarray, img2: np.ndarray, output_shape) -> np.ndarray: img1_resized = resize_and_pad(img1, output_shape) img2_resized = resize_and_pad(img2, output_shape) output = np.dstack((img1_resized, img2_resized)) return output # Example output_shape = (256, 128) img1 = np.ones((127, 47, 3)) * 128 img2 = np.ones((180, 57, 3)) * 64 out = create_instance(img1, img2, output_shape) #out has shape 256 x 128 x 6 print(np.shape(out))