import os
import random
from PIL import Image

# ✅ INPUT (your extracted dataset path)
SOURCE_DIR = r"F:\downloads\archive"

# ✅ OUTPUT (your project folder)
OUTPUT_DIR = r"C:\Users\Newpassword\PyCharmMiscProject\event-photo-delivery\dataset\events"

os.makedirs(OUTPUT_DIR, exist_ok=True)

# 🔍 Collect all images
all_images = []

for person in os.listdir(SOURCE_DIR):
    person_path = os.path.join(SOURCE_DIR, person)

    if os.path.isdir(person_path):
        for img in os.listdir(person_path):
            if img.lower().endswith((".jpg", ".jpeg", ".png")):
                all_images.append(os.path.join(person_path, img))

print(f"Total images found: {len(all_images)}")

# ⚙️ CONFIG
GROUP_SIZE = 4      # number of faces per image
NUM_GROUPS = 20     # number of group images to create

# 🧩 Create group images
for i in range(NUM_GROUPS):
    selected = random.sample(all_images, GROUP_SIZE)

    images = []
    for img_path in selected:
        try:
            img = Image.open(img_path).convert("RGB")
            img = img.resize((200, 200))
            images.append(img)
        except Exception as e:
            print(f"Skipping image: {img_path}")
            continue

    if len(images) < GROUP_SIZE:
        continue

    # Create blank canvas
    new_img = Image.new('RGB', (200 * GROUP_SIZE, 200))

    # Paste images
    for index, img in enumerate(images):
        new_img.paste(img, (index * 200, 0))

    # Save
    output_path = os.path.join(OUTPUT_DIR, f"group_{i}.jpg")
    new_img.save(output_path)

    print(f"✅ Created: {output_path}")

print("\n🎉 All group images created successfully!")