|
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) |
|
|
|
|