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.
Open the CMD and run the following command to install Pillow, a python image manipulation plugin.
pip install Pillow
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)
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
‘unterminated string literal’, change the folder location so windows properly recognises it, such as:
# pick ONE of the following:
folder = "D:\\\\" # normal string, escape the backslash
folder = r"D:\\\\" # raw string, double the trailing backslash
folder = "D:/" # forward slashes work on Windows too
folder = r"D:\\." # raw string that doesn't end with a backslash
Collage inside a collage
This means that you generated the collage twice, and thus had the first collage in the second collage. Remove the first image and repeat again, however refer to deleting images below if it still shows up.
Deleted files inside collage
Clear the recycle bin. This could be on the windows desktop, a hidden folder or a system folder that must be accessed through ⋮ → Clean Up → Recycle Bin (Windows 11)