from pathlib import Path from PIL import Image, ImageDraw, ImageFont, ImageOps src = Path(r"C:\Apache24\htdocs\mikrotik\admin\assets\id_cards") out = Path(r"C:\Apache24\htdocs\.codex_tmp\id_contacts") out.mkdir(parents=True, exist_ok=True) files = sorted(src.glob("bulk_*_both_*.jpg"), key=lambda p: int(p.name.split("_")[1])) font = ImageFont.load_default(size=24) cell_w, cell_h = 760, 240 cols, rows = 2, 5 for page, start in enumerate(range(0, len(files), cols * rows), 1): batch = files[start:start + cols * rows] canvas = Image.new("RGB", (cols * cell_w, rows * cell_h), "white") draw = ImageDraw.Draw(canvas) for i, path in enumerate(batch): img = Image.open(path).convert("RGB") crop = img.crop((0, 0, img.width, max(1, int(img.height * 0.24)))) crop.thumbnail((cell_w - 20, cell_h - 44)) x = (i % cols) * cell_w + 10 y = (i // cols) * cell_h + 38 canvas.paste(crop, (x, y)) sid = path.name.split("_")[1] draw.text((x, 6), f"DB ID {sid}", fill="black", font=font) canvas.save(out / f"contact_{page:02d}.jpg", quality=92) print(len(files), page)