# Hoja garter.
import pathlib
from PIL import Image, ImageDraw, ImageFont

BASE = pathlib.Path(r"C:\Users\Usuario\Downloads\SD")
SRC = BASE / "Output" / "test_garter"
files = sorted(SRC.glob("*.png"))
THUMB, PAD, LABEL_H = 450, 8, 30
thumbs = []
for p in files:
    im = Image.open(p).convert("RGB")
    r = THUMB / im.width
    thumbs.append((p.name, im.resize((THUMB, int(im.height * r)))))
W = 2 * (THUMB + PAD) + PAD
cell_h = max(t[1].height for t in thumbs) + LABEL_H + PAD * 2
H = cell_h + PAD
sheet = Image.new("RGB", (W, H), (18, 18, 18))
d = ImageDraw.Draw(sheet)
try:
    font = ImageFont.load_default(size=14)
except TypeError:
    font = ImageFont.load_default()
for i, (name, th) in enumerate(thumbs):
    x = PAD + i * (THUMB + PAD)
    y = PAD
    sheet.paste(th, (x, y + LABEL_H))
    d.text((x + 2, y + 4), name, fill=(255, 210, 120), font=font)
out = BASE / "Output" / "test_garter_sheet.jpg"
sheet.save(out, quality=88)
print(out, sheet.size)
