60 lines
2.2 KiB
Python
60 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Создать/обновить шаблон «Созидатели» из исходной картинки-примера.
|
|
|
|
Использование:
|
|
python examples/make_sozidateli_template.py "Сертификат Созидатели.png"
|
|
|
|
Скрипт:
|
|
* увеличивает фон x2 (для более чёткой печати),
|
|
* сохраняет его в data/templates/custom/sozidateli/background.png,
|
|
* регистрирует image-шаблон `sozidateli` с полем ФИО в нужной позиции.
|
|
В исходной картинке заголовок, «Подтверждает, что», основной текст и подпись
|
|
уже нарисованы — поэтому поверх накладывается только ФИО.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
|
|
from PIL import Image # noqa: E402
|
|
|
|
from certgen import config, store # noqa: E402
|
|
|
|
SLUG = "sozidateli"
|
|
SCALE = 2
|
|
LAYOUT = [
|
|
{"key": "fio", "x": 0.5, "y": 0.402, "size": 66, "font": "NotoSans-Bold",
|
|
"color": "#ffffff", "align": "center", "max_width": 0.88, "line_spacing": 1.15},
|
|
]
|
|
|
|
|
|
def main() -> None:
|
|
src = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("Сертификат Созидатели.png")
|
|
if not src.exists():
|
|
raise SystemExit(f"Не найден файл: {src}")
|
|
|
|
store.init_db()
|
|
img = Image.open(src).convert("RGB")
|
|
bg = img.resize((img.width * SCALE, img.height * SCALE), Image.LANCZOS)
|
|
bg_dir = config.TEMPLATES_DIR / "custom" / SLUG
|
|
bg_dir.mkdir(parents=True, exist_ok=True)
|
|
bg_path = bg_dir / "background.png"
|
|
bg.save(bg_path, "PNG")
|
|
|
|
tpl = store.upsert_template(
|
|
slug=SLUG,
|
|
title="Сертификат «Созидатели»",
|
|
kind="image",
|
|
background=str(bg_path.relative_to(config.TEMPLATES_DIR)),
|
|
layout=LAYOUT,
|
|
page={"width": bg.width, "height": bg.height},
|
|
defaults={"title": "СЕРТИФИКАТ", "body": ""},
|
|
)
|
|
print(f"Шаблон '{SLUG}' готов: {bg.width}x{bg.height}, фон {bg_path}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|