| def add_text(image, text, font_size=20, font_color=(255, 0, 0), font_path=None): |
| |
| draw = ImageDraw.Draw(image) |
|
|
| |
| if font_path: |
| font = ImageFont.truetype(font_path, font_size) |
| else: |
| font = ImageFont.load_default() |
|
|
| |
| text_width, text_height = draw.textsize(text, font=font) |
| width, height = image.size |
| text_x = (width - text_width) // 2 |
| text_y = 10 |
|
|
| |
| draw.text((text_x, text_y), text, font=font, fill=font_color) |
|
|
| def display_side_by_side_with_captions(image1_path, image2_path, output_path): |
| |
| image1 = Image.open(image1_path) |
| image2 = Image.open(image2_path) |
|
|
| |
| |
| width, height = image1.size |
| new_width = width * 2 |
| new_height = height |
|
|
| |
| image1 = image1.resize((new_width // 2, new_height)) |
| image2 = image2.resize((new_width // 2, new_height)) |
|
|
| |
| side_by_side = Image.new('RGB', (new_width, new_height)) |
|
|
| |
| side_by_side.paste(image1, (0, 0)) |
| side_by_side.paste(image2, (width, 0)) |
|
|
| |
| add_caption(image1, "Image 1", font_color=(255, 0, 0)) |
| add_caption(image2, "Image 2", font_color=(255, 0, 0)) |
|
|
| |
| side_by_side.save(output_path) |
|
|
| |
| if __name__ == "__main__": |
| image1_path = 'path_to_image1.jpg' |
| image2_path = 'path_to_image2.jpg' |
| output_path = 'side_by_side_with_captions.jpg' |
| display_side_by_side_with_captions(image1_path, image2_path, output_path) |
|
|
| print(f"Images displayed side by side with captions and saved as {output_path}") |