File size: 1,921 Bytes
8f1e0df fca5410 42df838 8f1e0df fcddb1d 1848ec3 8f1e0df fcddb1d 1848ec3 fcddb1d 1848ec3 fcddb1d 8f1e0df fcddb1d 8f1e0df fcddb1d 8f1e0df 1848ec3 8f1e0df fcddb1d 8f1e0df fcddb1d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import cv2 import numpy as np # Load the images image_paths = [ 'path_to_your_first_image.jpg', 'path_to_your_second_image.jpg', 'path_to_your_third_image.jpg', 'path_to_your_fourth_image.jpg', 'path_to_your_fifth_image.jpg' ] images = [cv2.imread(image_path) for image_path in image_paths] # Define the desired height for all images (e.g., maximum height among the images) desired_height = max(image.shape[0] for image in images) # Function to resize an image while maintaining aspect ratio def resize_with_aspect_ratio(image, height): aspect_ratio = image.shape[1] / image.shape[0] new_width = int(height * aspect_ratio) resized_image = cv2.resize(image, (new_width, height)) return resized_image # Resize images resized_images = [resize_with_aspect_ratio(image, desired_height) for image in images] # Add white padding to make the images the same width max_width = max(image.shape[1] for image in resized_images) padded_images = [cv2.copyMakeBorder( image, 0, 0, 0, max_width - image.shape[1], cv2.BORDER_CONSTANT, value=[255, 255, 255] ) for image in resized_images] # Combine images side by side combined_image = np.hstack(padded_images) # Add labels to the top of each image labels = ['Image 1', 'Image 2', 'Image 3', 'Image 4', 'Image 5'] font = cv2.FONT_HERSHEY_SIMPLEX font_scale = 1 color = (0, 0, 0) # Black color for the text thickness = 2 # Add labels to the combined image x_offset = 0 for i, label in enumerate(labels): label_size = cv2.getTextSize(label, font, font_scale, thickness)[0] x = x_offset + 10 y = label_size[1] + 10 cv2.putText(combined_image, label, (x, y), font, font_scale, color, thickness) x_offset += max_width # Display the combined image cv2.imshow('Combined Image', combined_image) cv2.waitKey(0) cv2.destroyAllWindows() # Save the combined image cv2.imwrite('combined_image.jpg', combined_image) |