/
axtonio
/
db_sbt
Обзор
Документация
Войти
/
axtonio
/
db_sbt
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
scripts/make_screenshots.py
70 строк
2 KB
axtonio
hw4
17 апр 2026, 23:35
17 апр 2026, 23:35
2226111
Код
Авторство
О чём код?
import argparse from pathlib import Path from typing import Any from PIL import Image, ImageDraw, ImageFont def load_font() -> Any: try: return ImageFont.truetype("/System/Library/Fonts/SFNSMono.ttf", 18) except Exception: return ImageFont.load_default() def render_text_to_png(text: str, output_path: Path, font: Any) -> None: lines = text.splitlines() or ["(empty output)"] lines = lines[:80] line_height = 24 padding = 24 dummy = Image.new("RGB", (10, 10)) draw = ImageDraw.Draw(dummy) max_width = 0 for line in lines: bbox = draw.textbbox((0, 0), line, font=font) max_width = max(max_width, bbox[2] - bbox[0]) width = int(min(max(1200, max_width + padding * 2), 2200)) height = int(padding * 2 + line_height * len(lines)) image = Image.new("RGB", (width, height), color=(20, 22, 30)) draw = ImageDraw.Draw(image) y = padding for line in lines: draw.text((padding, y), line, font=font, fill=(230, 232, 240)) y += line_height output_path.parent.mkdir(parents=True, exist_ok=True) image.save(output_path) def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser() parser.add_argument("--hw", default="hw3") return parser.parse_args() def main() -> None: args = parse_args() repo_root = Path(__file__).resolve().parents[1] artifacts_dir = repo_root / args.hw / "artifacts" screenshots_dir = repo_root / args.hw / "screenshots" font = load_font() txt_files = sorted(artifacts_dir.glob("*.txt")) if not txt_files: print("no .txt files found in artifacts") return for src in txt_files: text = src.read_text(encoding="utf-8", errors="replace") dst = screenshots_dir / f"{src.stem}.png" render_text_to_png(text, dst, font) print(f"ok: {src.name} -> {dst.name}") if __name__ == "__main__": main()