Update image_display.py
Browse files- image_display.py +27 -6
image_display.py
CHANGED
|
@@ -1,6 +1,23 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# Open images using Pillow
|
| 5 |
image1 = Image.open(image1_path)
|
| 6 |
image2 = Image.open(image2_path)
|
|
@@ -22,6 +39,10 @@ def display_side_by_side(image1_path, image2_path, output_path):
|
|
| 22 |
side_by_side.paste(image1, (0, 0))
|
| 23 |
side_by_side.paste(image2, (width, 0))
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# Save the side by side image to a file
|
| 26 |
side_by_side.save(output_path)
|
| 27 |
|
|
@@ -29,7 +50,7 @@ def display_side_by_side(image1_path, image2_path, output_path):
|
|
| 29 |
if __name__ == "__main__":
|
| 30 |
image1_path = 'path_to_image1.jpg'
|
| 31 |
image2_path = 'path_to_image2.jpg'
|
| 32 |
-
output_path = '
|
| 33 |
-
|
| 34 |
|
| 35 |
-
print(f"Images displayed side by side and saved as {output_path}")
|
|
|
|
| 1 |
+
def add_caption(image, caption, font_size=20, font_color=(255, 0, 0), font_path=None):
|
| 2 |
+
# Create a drawing context
|
| 3 |
+
draw = ImageDraw.Draw(image)
|
| 4 |
+
|
| 5 |
+
# Load a font
|
| 6 |
+
if font_path:
|
| 7 |
+
font = ImageFont.truetype(font_path, font_size)
|
| 8 |
+
else:
|
| 9 |
+
font = ImageFont.load_default()
|
| 10 |
+
|
| 11 |
+
# Calculate text size and position
|
| 12 |
+
text_width, text_height = draw.textsize(caption, font=font)
|
| 13 |
+
width, height = image.size
|
| 14 |
+
text_x = (width - text_width) // 2
|
| 15 |
+
text_y = 10 # Adjust this value for vertical position
|
| 16 |
+
|
| 17 |
+
# Draw text on image
|
| 18 |
+
draw.text((text_x, text_y), caption, font=font, fill=font_color)
|
| 19 |
+
|
| 20 |
+
def display_side_by_side_with_captions(image1_path, image2_path, output_path):
|
| 21 |
# Open images using Pillow
|
| 22 |
image1 = Image.open(image1_path)
|
| 23 |
image2 = Image.open(image2_path)
|
|
|
|
| 39 |
side_by_side.paste(image1, (0, 0))
|
| 40 |
side_by_side.paste(image2, (width, 0))
|
| 41 |
|
| 42 |
+
# Add captions to each image
|
| 43 |
+
add_caption(image1, "Image 1", font_color=(255, 0, 0))
|
| 44 |
+
add_caption(image2, "Image 2", font_color=(255, 0, 0))
|
| 45 |
+
|
| 46 |
# Save the side by side image to a file
|
| 47 |
side_by_side.save(output_path)
|
| 48 |
|
|
|
|
| 50 |
if __name__ == "__main__":
|
| 51 |
image1_path = 'path_to_image1.jpg'
|
| 52 |
image2_path = 'path_to_image2.jpg'
|
| 53 |
+
output_path = 'side_by_side_with_captions.jpg'
|
| 54 |
+
display_side_by_side_with_captions(image1_path, image2_path, output_path)
|
| 55 |
|
| 56 |
+
print(f"Images displayed side by side with captions and saved as {output_path}")
|