Update New_file.txt
Browse files- New_file.txt +27 -37
New_file.txt
CHANGED
|
@@ -2,13 +2,17 @@ import cv2
|
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
# Load the images
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Function to resize an image while maintaining aspect ratio
|
| 14 |
def resize_with_aspect_ratio(image, height):
|
|
@@ -18,50 +22,35 @@ def resize_with_aspect_ratio(image, height):
|
|
| 18 |
return resized_image
|
| 19 |
|
| 20 |
# Resize images
|
| 21 |
-
|
| 22 |
-
image2_resized = resize_with_aspect_ratio(image2, desired_height)
|
| 23 |
|
| 24 |
# Add white padding to make the images the same width
|
| 25 |
-
max_width = max(
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
0, 0, 0, max_width -
|
| 29 |
-
cv2.BORDER_CONSTANT,
|
| 30 |
-
value=[255, 255, 255]
|
| 31 |
-
)
|
| 32 |
-
image2_padded = cv2.copyMakeBorder(
|
| 33 |
-
image2_resized,
|
| 34 |
-
0, 0, 0, max_width - image2_resized.shape[1],
|
| 35 |
cv2.BORDER_CONSTANT,
|
| 36 |
value=[255, 255, 255]
|
| 37 |
-
)
|
| 38 |
|
| 39 |
# Combine images side by side
|
| 40 |
-
combined_image = np.hstack(
|
| 41 |
|
| 42 |
# Add labels to the top of each image
|
| 43 |
-
|
| 44 |
-
label2 = 'Image 2'
|
| 45 |
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 46 |
font_scale = 1
|
| 47 |
color = (0, 0, 0) # Black color for the text
|
| 48 |
thickness = 2
|
| 49 |
|
| 50 |
-
# Calculate the position for the labels
|
| 51 |
-
label1_size = cv2.getTextSize(label1, font, font_scale, thickness)[0]
|
| 52 |
-
label2_size = cv2.getTextSize(label2, font, font_scale, thickness)[0]
|
| 53 |
-
|
| 54 |
-
# Position for label1
|
| 55 |
-
x1 = 10
|
| 56 |
-
y1 = label1_size[1] + 10
|
| 57 |
-
|
| 58 |
-
# Position for label2
|
| 59 |
-
x2 = image1_padded.shape[1] + 10
|
| 60 |
-
y2 = label2_size[1] + 10
|
| 61 |
-
|
| 62 |
# Add labels to the combined image
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Display the combined image
|
| 67 |
cv2.imshow('Combined Image', combined_image)
|
|
@@ -70,3 +59,4 @@ cv2.destroyAllWindows()
|
|
| 70 |
|
| 71 |
# Save the combined image
|
| 72 |
cv2.imwrite('combined_image.jpg', combined_image)
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
# Load the images
|
| 5 |
+
image_paths = [
|
| 6 |
+
'path_to_your_first_image.jpg',
|
| 7 |
+
'path_to_your_second_image.jpg',
|
| 8 |
+
'path_to_your_third_image.jpg',
|
| 9 |
+
'path_to_your_fourth_image.jpg',
|
| 10 |
+
'path_to_your_fifth_image.jpg'
|
| 11 |
+
]
|
| 12 |
+
images = [cv2.imread(image_path) for image_path in image_paths]
|
| 13 |
+
|
| 14 |
+
# Define the desired height for all images (e.g., maximum height among the images)
|
| 15 |
+
desired_height = max(image.shape[0] for image in images)
|
| 16 |
|
| 17 |
# Function to resize an image while maintaining aspect ratio
|
| 18 |
def resize_with_aspect_ratio(image, height):
|
|
|
|
| 22 |
return resized_image
|
| 23 |
|
| 24 |
# Resize images
|
| 25 |
+
resized_images = [resize_with_aspect_ratio(image, desired_height) for image in images]
|
|
|
|
| 26 |
|
| 27 |
# Add white padding to make the images the same width
|
| 28 |
+
max_width = max(image.shape[1] for image in resized_images)
|
| 29 |
+
padded_images = [cv2.copyMakeBorder(
|
| 30 |
+
image,
|
| 31 |
+
0, 0, 0, max_width - image.shape[1],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
cv2.BORDER_CONSTANT,
|
| 33 |
value=[255, 255, 255]
|
| 34 |
+
) for image in resized_images]
|
| 35 |
|
| 36 |
# Combine images side by side
|
| 37 |
+
combined_image = np.hstack(padded_images)
|
| 38 |
|
| 39 |
# Add labels to the top of each image
|
| 40 |
+
labels = ['Image 1', 'Image 2', 'Image 3', 'Image 4', 'Image 5']
|
|
|
|
| 41 |
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 42 |
font_scale = 1
|
| 43 |
color = (0, 0, 0) # Black color for the text
|
| 44 |
thickness = 2
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
# Add labels to the combined image
|
| 47 |
+
x_offset = 0
|
| 48 |
+
for i, label in enumerate(labels):
|
| 49 |
+
label_size = cv2.getTextSize(label, font, font_scale, thickness)[0]
|
| 50 |
+
x = x_offset + 10
|
| 51 |
+
y = label_size[1] + 10
|
| 52 |
+
cv2.putText(combined_image, label, (x, y), font, font_scale, color, thickness)
|
| 53 |
+
x_offset += max_width
|
| 54 |
|
| 55 |
# Display the combined image
|
| 56 |
cv2.imshow('Combined Image', combined_image)
|
|
|
|
| 59 |
|
| 60 |
# Save the combined image
|
| 61 |
cv2.imwrite('combined_image.jpg', combined_image)
|
| 62 |
+
|