1. Description:

This python program generates an image by scanning a select folder and then collaging every single .png and .jpg in that folder into 1 image. It also scales all the images to 300x300 pixels to save space and not take an extremely long time to generate.

2. Install Pillow:

Open the CMD and run the following command to install Pillow, a python image manipulation plugin.

pip install Pillow

3. Create code:

In a new python file, copy and paste this code.

import os
from PIL import Image
import math

def make_collage(root_folder, output_name="collage.jpg", thumb_size=300):
    # Gather all images (png and jpg) recursively
    image_paths = []
    for dirpath, _, filenames in os.walk(root_folder):
        for f in filenames:
            if f.lower().endswith((".png", ".jpg", ".jpeg")):
                image_paths.append(os.path.join(dirpath, f))

    if not image_paths:
        print("No images found.")
        return

    print(f"Found {len(image_paths)} images.")

    # Open and resize all images to thumb_size x thumb_size
    images = []
    for path in image_paths:
        try:
            img = Image.open(path).convert("RGB")
            img = img.resize((thumb_size, thumb_size), Image.LANCZOS)
            images.append(img)
        except Exception as e:
            print(f"Skipping {path}: {e}")

    if not images:
        print("No valid images to process.")
        return

    # Calculate grid size (square layout)
    cols = math.ceil(math.sqrt(len(images)))
    rows = math.ceil(len(images) / cols)

    # Create blank canvas
    collage_w = cols * thumb_size
    collage_h = rows * thumb_size
    collage = Image.new("RGB", (collage_w, collage_h), (0, 0, 0))

    # Paste images into grid
    for idx, img in enumerate(images):
        x = (idx % cols) * thumb_size
        y = (idx // cols) * thumb_size
        collage.paste(img, (x, y))

    # Save collage
    out_path = os.path.join(root_folder, output_name)
    collage.save(out_path, "JPEG", quality=95)
    print(f"Collage saved at {out_path}")

# Example usage
if __name__ == "__main__":
    folder = r"D:\\\\" #YOUR FOLDER
    make_collage(folder)

4. Select folder and run:

Now, change the code in the very bottom as shown below to your selected folder, and then run the code to generate the image in the folder you selected.

folder = r"D:\\\\" #YOUR FOLDER

5. Possible issues: