#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""contact_korea.py — inventory + contact sheet foto Korea di openclaw-img."""
import os, sys
from PIL import Image, ImageDraw

IMG = "/home/mantapman/.hermes/profiles/atun/workspace/openclaw-img"
OUT = "/tmp/korea_sheet"
os.makedirs(OUT, exist_ok=True)
seri = sys.argv[1] if len(sys.argv) > 1 else "Bukchon"
cols = 5
thumb = (300, 300)

files = sorted(f for f in os.listdir(IMG)
               if f.lower().startswith(seri.lower()) and f.lower().endswith((".jpg", ".jpeg", ".png")))
print(f"{seri}: {len(files)} file")
for f in files:
    with Image.open(os.path.join(IMG, f)) as im:
        print(f"  {f:32} {im.size[0]}x{im.size[1]} {'tegak' if im.size[1] > im.size[0] else ('datar' if im.size[0] > im.size[1] else 'persegi')}")

rows = (len(files) + cols - 1) // cols
sheet = Image.new("RGB", (cols * thumb[0], rows * (thumb[1] + 22)), "white")
d = ImageDraw.Draw(sheet)
for i, f in enumerate(files):
    im = Image.open(os.path.join(IMG, f)).convert("RGB")
    im.thumbnail(thumb)
    x = (i % cols) * thumb[0] + (thumb[0] - im.size[0]) // 2
    y = (i // cols) * (thumb[1] + 22)
    sheet.paste(im, (x, y))
    d.text(((i % cols) * thumb[0] + 4, y + thumb[1] + 4), f[:40], fill="black")
p = os.path.join(OUT, f"{seri}.jpg")
sheet.save(p, quality=88)
print("contact sheet ->", p)
