singe/util/forgeShots.py
2026-09-14 22:32:53 -05:00

38 lines
1.3 KiB
Python

#!/usr/bin/env python3
# Puts the tutorial pictures into the book: every NN-name.png a tutorial scene took (in
# screenshots/tutN/, where the harness leaves them) is copied to docs/images/tutorials/, scaled to
# the width a page needs and reduced to a palette, so ten tutorials of full-window screenshots do
# not make Forge.pdf ten times its size.
#
# python3 util/forgeShots.py [screenshots/tut1 ...]
#
# With no arguments every screenshots/tut* folder is taken.
import glob
import os
import sys
from PIL import Image
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
OUT = os.path.join(ROOT, "docs", "images", "tutorials")
WIDTH = 768
def place(path):
img = Image.open(path).convert("RGB")
if img.width > WIDTH:
img = img.resize((WIDTH, img.height * WIDTH // img.width), Image.LANCZOS)
img = img.quantize(colors=128, method=Image.Quantize.MEDIANCUT)
img.save(os.path.join(OUT, os.path.basename(path)), optimize=True)
if __name__ == "__main__":
folders = sys.argv[1:] or sorted(glob.glob(os.path.join(ROOT, "screenshots", "tut*")))
os.makedirs(OUT, exist_ok=True)
count = 0
for folder in folders:
for path in sorted(glob.glob(os.path.join(folder, "[0-9][0-9]-*.png"))):
place(path)
count += 1
print("%d pictures into %s" % (count, OUT))