File size: 1,133 Bytes
51557c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from PIL import Image


def stack_images(image_paths, output_path):
    # Load all images from the provided paths
    images = [Image.open(path) for path in image_paths]

    # Determine the size of individual images (assuming all are the same size)
    width, height = images[0].size

    # Create a new image with appropriate size (2 columns and 3 rows)
    total_width = width * 2
    total_height = height * 3
    new_image = Image.new("RGB", (total_width, total_height))

    # Paste each image into the new image
    for i, image in enumerate(images):
        # Calculate the position for each image
        x_offset = (i % 2) * width
        y_offset = (i // 2) * height
        new_image.paste(image, (x_offset, y_offset))

    # Save the new image
    new_image.save(output_path)


# Example usage
image_paths = [
    "/Users/mav/Desktop/example1.png",
    "/Users/mav/Desktop/image-1.webp",
    "/Users/mav/Desktop/example2.png",
    "/Users/mav/Desktop/image-2.webp",
    "/Users/mav/Desktop/example3.png",
    "/Users/mav/Desktop/image-3.webp",
]
output_path = "stacked_images.jpg"
stack_images(image_paths, output_path)