Something went wrong. Try again.
slack status without the slack status.zzstoatzz.io
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132#!/usr/bin/env python3"""render the link-preview cards for pages that have no image of their own.
run: uv run --no-project --with pillow scripts/make-og-image.py
writes static/og.png (home) and static/og-feed.png. pillow and the bufodownloads are build-time only — the committed pngs are what ship, so nothinghere reaches the app or the docker image.
1200x630 because that is the ratio every scraper crops to. an earlier versionof this card was a 512px square logo, which unfurled as a giant cropped circle;the bufo is composed *into* the card for the same reason, rather than beingserved as the og:image itself."""
import urllib.requestfrom io import BytesIOfrom pathlib import Path
from PIL import Image, ImageDraw, ImageFont
W, H = 1200, 630BG = "#0a0a0a" # --bgCARD = "#1a1a1a" # --bg-cardBORDER = "#2a2a2a" # --borderTEXT = "#ffffff" # --textMUTED = "#888888" # --text-secondaryACCENT = "#4a9eff" # --accent
PAD = 90TILE = 320TEXT_W = W - PAD * 3 - TILE
# the app is ui-monospace; SF Mono is what that resolves to on macOSFONT_CANDIDATES = [ "/System/Library/Fonts/SFNSMono.ttf", "/System/Library/Fonts/Menlo.ttc", "/System/Library/Fonts/Supplemental/Andale Mono.ttf",]
CARDS = [ ("og.png", "status", "slack status without the slack", "be-the-bufo-you-want-to-see"), ( "og-feed.png", "global feed", "what everyone is up to right now", "whatever-youre-doing-its-attracting-the-bufos", ),]
def font(size: int, weight: str | None = None): for path in FONT_CANDIDATES: try: f = ImageFont.truetype(path, size) except OSError: continue if weight: try: f.set_variation_by_name(weight) except (OSError, AttributeError): pass # static font — the regular cut is fine return f raise SystemExit("no monospace font found")
def fitted(text: str, start: int, weight: str | None = None): """the largest size at which the line still fits the text column""" probe = ImageDraw.Draw(Image.new("RGB", (1, 1))) size = start while size > 24: f = font(size, weight) if probe.textlength(text, font=f) <= TEXT_W: return f size -= 2 return font(24, weight)
def bufo(name: str) -> Image.Image: """/e/ owns resolution across bufo.zone's png and gif sources — see emoji.ts""" url = f"https://find-bufo.com/e/{name}.png" # urllib's default user-agent gets a 403 here; identify ourselves instead req = urllib.request.Request(url, headers={"User-Agent": "status-og-card/1.0"}) with urllib.request.urlopen(req) as r: # noqa: S310 — fixed https host im = Image.open(BytesIO(r.read())) if im.format == "GIF": im.seek(0) return im.convert("RGBA")
def render(filename: str, wordmark: str, tagline: str, bufo_name: str, dest: Path): img = Image.new("RGB", (W, H), BG) d = ImageDraw.Draw(img)
# the bufo fills a rounded tile like a framed photo. it has to *fill* it: # some bufos are cut out on transparency and some are opaque scenes, and # insetting the art leaves the opaque ones looking like a picture in a # mismatched frame. the tile colour still shows through the cut-out ones. tx, ty = W - PAD - TILE, (H - TILE) // 2 tile = Image.new("RGBA", (TILE, TILE), CARD) art = bufo(bufo_name).resize((TILE, TILE), Image.LANCZOS) tile.alpha_composite(art)
mask = Image.new("L", (TILE, TILE), 0) ImageDraw.Draw(mask).rounded_rectangle([0, 0, TILE - 1, TILE - 1], radius=24, fill=255) img.paste(tile.convert("RGB"), (tx, ty), mask) d.rounded_rectangle( [tx, ty, tx + TILE - 1, ty + TILE - 1], radius=24, outline=BORDER, width=2 )
wm, tg = fitted(wordmark, 122, "Bold"), fitted(tagline, 36) wb = d.textbbox((0, 0), wordmark, font=wm) tb = d.textbbox((0, 0), tagline, font=tg) rule_gap, rule_h, tag_gap = 40, 7, 38 block = (wb[3] - wb[1]) + rule_gap + rule_h + tag_gap + (tb[3] - tb[1]) top = (H - block) // 2
d.text((PAD, top - wb[1]), wordmark, font=wm, fill=TEXT) rule_y = top + (wb[3] - wb[1]) + rule_gap d.rounded_rectangle([PAD, rule_y, PAD + 80, rule_y + rule_h], radius=4, fill=ACCENT) d.text((PAD, rule_y + rule_h + tag_gap - tb[1]), tagline, font=tg, fill=MUTED)
out = dest / filename img.save(out, "PNG", optimize=True) print(f"wrote {out} ({out.stat().st_size} bytes, {W}x{H})")
if __name__ == "__main__": static = Path(__file__).resolve().parent.parent / "static" for spec in CARDS: render(*spec, static)