import os import math from PIL import Image, ImageDraw folder = 'public/assets/photos' files = [f for f in os.listdir(folder) if f.lower().endswith(('.jpg', '.jpeg', '.png'))] files.sort() thumb_w, thumb_h = 300, 300 cols = 8 rows = math.ceil(len(files) / cols) contact_sheet = Image.new('RGB', (cols * thumb_w, rows * thumb_h), (255, 255, 255)) draw = ImageDraw.Draw(contact_sheet) for i, f in enumerate(files): path = os.path.join(folder, f) try: img = Image.open(path) img.thumbnail((thumb_w, thumb_h - 40)) x = (i % cols) * thumb_w y = (i // cols) * thumb_h img_x = x + (thumb_w - img.width) // 2 img_y = y + (thumb_h - 40 - img.height) // 2 contact_sheet.paste(img, (img_x, img_y)) draw.text((x + 10, y + thumb_h - 30), f, fill=(0,0,0)) except Exception as e: pass contact_sheet.save('contact_sheet.jpg') print("Saved contact_sheet.jpg")