#!/usr/bin/env python3
"""Contact sheet kedua: 12 kandidat terpilih, sel lebih besar supaya terbaca."""
import os
from PIL import Image, ImageDraw, ImageFont

OUT = "/home/mantapman/.hermes/profiles/atun/workspace/tslaw_listikel"
pilih = ["4910129", "7350910", "7563461", "4910124", "4977481", "11594551",
         "8730275", "16603615", "7876050", "5673490", "3927131", "37568245"]
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 26)
cols, cw, ch = 3, 440, 250
rows = (len(pilih) + cols - 1) // cols
sheet = Image.new("RGB", (cols * cw, rows * (ch + 32)), "white")
dr = ImageDraw.Draw(sheet)
for n, i in enumerate(pilih):
    f = f"{OUT}/_kand_{i}.jpg"
    if not os.path.exists(f):
        print("hilang", i); continue
    im = Image.open(f).convert("RGB")
    w, h = im.size
    r = cw / ch
    if w / h > r:
        nw = int(h * r); im = im.crop(((w - nw) // 2, 0, (w - nw) // 2 + nw, h))
    else:
        nh = int(w / r); im = im.crop((0, (h - nh) // 2, w, (h - nh) // 2 + nh))
    im = im.resize((cw, ch), Image.LANCZOS)
    x = (n % cols) * cw; y = (n // cols) * (ch + 32)
    sheet.paste(im, (x, y))
    dr.text((x + 8, y + ch + 4), i, fill="black", font=font)
sheet.save(f"{OUT}/_sheet2.jpg", quality=90)
print(f"{OUT}/_sheet2.jpg {sheet.size}")
