from PIL import Image, ImageDraw, ImageFont import os # Configuration directory = "C:\blah" # Change this to the path of your images output_image_path = "C:\blah.jpg" # Change this to your desired output path thumb_size = (384, 512) # Size of each thumbnail canvas_size = (1192, 64000) # Size of the output image canvas, change to fit all of your images columns = 3 # Number of columns for thumbnails padding = 10 # Padding between images and text # Optionally specify a font for the filenames # On Windows, you might find fonts in C:\\Windows\\Fonts font = ImageFont.truetype("arial.ttf", 20) # If you don't have access to a specific font or are unsure, you can use a default font #font = ImageFont.load_default() # Create a blank canvas canvas = Image.new('RGB', canvas_size, (255, 255, 255)) draw = ImageDraw.Draw(canvas) x_offset = 0 y_offset = 0 next_row_y = 0 column_count = 0 # Process each image in the directory for filename in os.listdir(directory): if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')): try: with Image.open(os.path.join(directory, filename)) as img: img.thumbnail(thumb_size) text_size = draw.textsize(filename, font=font) img_with_text_height = thumb_size[1] + text_size[1] + padding # Check if it's time to move to a new row if column_count >= columns: x_offset = 0 y_offset = next_row_y column_count = 0 # Paste the image canvas.paste(img, (x_offset, y_offset)) # Draw the filename below the image draw.text((x_offset, y_offset + thumb_size[1] + padding), filename, fill=(0, 0, 0), font=font) # Update offsets column_count += 1 x_offset += max(thumb_size[0], text_size[0]) + padding next_row_y = max(next_row_y, y_offset + img_with_text_height + padding) if next_row_y + img_with_text_height > canvas_size[1]: print("Canvas filled up! Saving...") break except Exception as e: print(f"Error processing {filename}: {e}") # Save the canvas canvas.save(output_image_path) print(f"Collage created at {output_image_path}")