Spaces:
Running
on
Zero
Running
on
Zero
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) | |