|
import cv2 |
|
import numpy as np |
|
|
|
# Load the images |
|
image1_path = 'path_to_your_first_image.jpg' |
|
image2_path = 'path_to_your_second_image.jpg' |
|
image1 = cv2.imread(image1_path) |
|
image2 = cv2.imread(image2_path) |
|
|
|
# Resize images to the same height for concatenation |
|
height1, width1, _ = image1.shape |
|
height2, width2, _ = image2.shape |
|
|
|
# Define the desired height for both images (e.g., height of the first image) |
|
desired_height = height1 |
|
|
|
# Resize images |
|
image1_resized = cv2.resize(image1, (width1, desired_height)) |
|
image2_resized = cv2.resize(image2, (width2, desired_height)) |
|
|
|
# Combine images side by side |
|
combined_image = np.hstack((image1_resized, image2_resized)) |
|
|
|
# Add labels to the top of each image |
|
label1 = 'Image 1' |
|
label2 = 'Image 2' |
|
font = cv2.FONT_HERSHEY_SIMPLEX |
|
font_scale = 1 |
|
color = (255, 255, 255) # White color for the text |
|
thickness = 2 |
|
|
|
# Calculate the position for the labels |
|
label1_size = cv2.getTextSize(label1, font, font_scale, thickness)[0] |
|
label2_size = cv2.getTextSize(label2, font, font_scale, thickness)[0] |
|
|
|
# Position for label1 |
|
x1 = 10 |
|
y1 = label1_size[1] + 10 |
|
|
|
# Position for label2 |
|
x2 = width1 + 10 |
|
y2 = label2_size[1] + 10 |
|
|
|
# Add labels to the combined image |
|
cv2.putText(combined_image, label1, (x1, y1), font, font_scale, color, thickness) |
|
cv2.putText(combined_image, label2, (x2, y2), font, font_scale, color, thickness) |
|
|
|
# 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) |
|
|