build: add render_title_cards.py — unified gold hollow-outline title cards
This commit is contained in:
1 parent
5380bbc133
commit
6add0a9732
1 file changed
+119
Executable
+119
@@ -0,0 +1,119 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Render unified Liberty Tools title cards.
|
||||
|
||||
Every section page's header title is text baked into a JPEG (there is no HTML
|
||||
heading above the image). This script re-renders all of them with ONE
|
||||
consistent recipe so the pages look uniform:
|
||||
|
||||
- fixed card size 1046x124 (matches the Death Athletes reference)
|
||||
- black background
|
||||
- gold (221,177,57) HOLLOW-OUTLINE slab-serif text (AlfaSlabOne)
|
||||
- 76px text height (matches the Death Athletes reference)
|
||||
- text centered horizontally and vertically
|
||||
|
||||
It writes content/Images/Headers/<Name>.jpg for each page in the PAGE map and
|
||||
adds the previously-missing Books card. The second image under each title
|
||||
(Artwork/*.jpg) is intentionally NOT touched.
|
||||
|
||||
Usage: python3 scripts/render_title_cards.py
|
||||
Verify: python3 scripts/render_title_cards.py --check
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
FONT = os.path.join(REPO, "static", "fonts", "AlfaSlabOne.ttf")
|
||||
HDR_DIR = os.path.join(REPO, "content", "Images", "Headers")
|
||||
|
||||
GOLD = (221, 177, 57)
|
||||
BLACK = (0, 0, 0)
|
||||
CARD_W, CARD_H = 1046, 124 # uniform card box (Death Athletes reference)
|
||||
TEXT_H = 76 # uniform text height (Death Athletes reference)
|
||||
STROKE = 3 # gold outline thickness
|
||||
|
||||
# page -> (title text, output filename in Headers/)
|
||||
# Books is NEW (the page had no header before). All others overwrite the
|
||||
# existing file so the .md references need no changes.
|
||||
PAGES = {
|
||||
"agorism": ("AGORISM", "Agorism.jpg"),
|
||||
"books": ("BOOKS", "Books.jpg"),
|
||||
"covid": ("COVID", "Covid.jpg"),
|
||||
"deathathletes": ("DEATH ATHLETES", "Death.jpg"),
|
||||
"defense": ("DEFENSE", "defense2.jpg"),
|
||||
"education": ("EDUCATION", "education.jpg"),
|
||||
"frontends": ("FRONT ENDS", "frontends.jpg"),
|
||||
"hacking": ("HACKING", "Hacking.jpg"),
|
||||
"health": ("HEALTH", "health.jpg"),
|
||||
"job-boards": ("JOB BOARDS", "JobBoards.jpg"),
|
||||
"metrics": ("METRICS", "Metrics.jpg"),
|
||||
"money": ("MONEY", "Money.jpg"),
|
||||
"organizations": ("ORGANIZATIONS", "orgs.jpg"),
|
||||
"osint": ("OSINT", "OSINT.jpg"),
|
||||
"political-tools": ("POLITICAL", "Political.jpg"),
|
||||
"privacy": ("PRIVACY", "privacy.jpg"),
|
||||
"self-hosting": ("SELF HOSTING", "SelfHosting1.jpg"),
|
||||
"social-media": ("SOCIAL MEDIA", "socialmedia3.jpg"),
|
||||
"affiliates": ("AFFILIATES", "Affiliates2.jpg"),
|
||||
}
|
||||
|
||||
|
||||
def font_size_for(text: str) -> int:
|
||||
"""Largest font size whose text height is <= TEXT_H (binary search)."""
|
||||
lo, hi, best = 20, 400, 20
|
||||
while lo <= hi:
|
||||
mid = (lo + hi) // 2
|
||||
f = ImageFont.truetype(FONT, mid)
|
||||
h = f.getbbox(text)[3] - f.getbbox(text)[1]
|
||||
if h <= TEXT_H:
|
||||
best, lo = mid, mid + 1
|
||||
else:
|
||||
hi = mid - 1
|
||||
return best
|
||||
|
||||
|
||||
def render(title: str, out_path: str, write: bool = True) -> dict:
|
||||
size = font_size_for(title)
|
||||
f = ImageFont.truetype(FONT, size)
|
||||
l, t, r, b = f.getbbox(title)
|
||||
img = Image.new("RGB", (CARD_W, CARD_H), BLACK)
|
||||
d = ImageDraw.Draw(img)
|
||||
# center the text in the fixed card box
|
||||
x = (CARD_W - (r - l)) // 2 - l
|
||||
y = (CARD_H - (b - t)) // 2 - t
|
||||
# hollow gold: black fill + gold outline stroke
|
||||
d.text((x, y), title, font=f, fill=BLACK, stroke_width=STROKE, stroke_fill=GOLD)
|
||||
if write:
|
||||
img.save(out_path, quality=92)
|
||||
# measure the rendered gold bbox to report centering
|
||||
g = img.convert("L").point(lambda v: 255 if v > 40 else 0)
|
||||
bx1, by1, bx2, by2 = g.getbbox()
|
||||
return {
|
||||
"title": title, "font_size": size,
|
||||
"text_w": bx2 - bx1, "text_h": by2 - by1,
|
||||
"left_gap": bx1, "right_gap": CARD_W - bx2,
|
||||
"centered_x": abs(bx1 - (CARD_W - bx2)) <= 3,
|
||||
"fits": bx2 - bx1 <= CARD_W,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--check", action="store_true",
|
||||
help="report each card's geometry without writing files")
|
||||
args = ap.parse_args()
|
||||
|
||||
for page, (title, fname) in PAGES.items():
|
||||
out = os.path.join(HDR_DIR, fname)
|
||||
if args.check:
|
||||
r = render(title, out, write=False)
|
||||
flag = "OK" if (r["centered_x"] and r["fits"]) else "PROBLEM"
|
||||
print(f"[{flag:7}] {page:16} {title:16} font={r['font_size']:3} "
|
||||
f"text_w={r['text_w']:4} L={r['left_gap']:3} R={r['right_gap']:3}")
|
||||
else:
|
||||
r = render(title, out)
|
||||
print(f"wrote {out} (font={r['font_size']}, text_w={r['text_w']})")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user