#!/usr/bin/env python3 """Render the Substack-ready lexical pivots card from curated excerpt rows.""" from __future__ import annotations import argparse import json import sys from pathlib import Path from typing import Any from PIL import Image, ImageDraw, ImageFont WIDTH = 1500 SCALE = 2 FONT_PATHS = { "georgia": "/System/Library/Fonts/Supplemental/Georgia.ttf", "georgia_bold": "/System/Library/Fonts/Supplemental/Georgia Bold.ttf", "arial": "/System/Library/Fonts/Supplemental/Arial.ttf", "arial_bold": "/System/Library/Fonts/Supplemental/Arial Bold.ttf", } DISPLAY_ROWS = [ { "rank": "1", "ticker": "NKE", "display_name": "Nike", "rows": [ ("Pricing", "pricing_power", "reduced promotional activity", "digital is still too promotional"), ("Inventory", "inventory", "clean marketplace inventory levels", "Markdowns across the marketplace remain elevated"), ("Inventory", "inventory", "clean marketplace inventory levels", "elevated inventory"), ], }, { "rank": "2", "ticker": "IR", "display_name": "Ingersoll Rand", "rows": [ ("Demand", "demand", "finished the year strong", "longer cycle projects being delayed"), ("Pricing", "pricing_power", "targeted pricing actions", "demand elasticity based on price"), ("Guidance", "guidance_answer_drop", "we expect Q1 organic to be", "order recovery throughout the year"), ], }, { "rank": "3", "ticker": "ALGN", "display_name": "Align Technology", "rows": [ ("Demand", "demand", "strong growth", "retail channel continued to be mixed"), ("Demand", "demand", "continued strength", "less patient traffic"), ("Guidance", "guidance", "future events and product outlook", "puts and takes as you go through any quarter"), ], }, ] def load_jsonl(path: Path) -> list[dict[str, Any]]: rows: list[dict[str, Any]] = [] with path.open("r", encoding="utf-8") as handle: for line in handle: line = line.strip() if line: rows.append(json.loads(line)) return rows def find_excerpt(rows: list[dict[str, Any]], ticker: str, finding: str, prior: str, latest: str) -> dict[str, Any]: for row in rows: if ( row.get("ticker") == ticker and row.get("finding") == finding and row.get("prior_excerpt") == prior and row.get("latest_excerpt") == latest ): return row raise RuntimeError(f"Missing excerpt row for {ticker} {finding}: {prior!r} -> {latest!r}") def font(name: str, size: int) -> ImageFont.FreeTypeFont: path = FONT_PATHS[name] if not Path(path).exists(): raise RuntimeError(f"Required font missing: {path}") return ImageFont.truetype(path, size * SCALE) def draw_wrapped( draw: ImageDraw.ImageDraw, text: str, x: int, y: int, font_obj: ImageFont.FreeTypeFont, fill: str, width: int, line_gap: int = 5, max_lines: int = 2, ) -> int: words = text.split() lines: list[str] = [] line = "" for word in words: test = (line + " " + word).strip() if draw.textlength(test, font=font_obj) <= width: line = test else: if line: lines.append(line) line = word if line: lines.append(line) lines = lines[:max_lines] yy = y bbox = draw.textbbox((0, 0), "Ag", font=font_obj) line_height = bbox[3] - bbox[1] + line_gap * SCALE for line in lines: draw.text((x, yy), line, font=font_obj, fill=fill) yy += line_height return yy def render(input_path: Path, output_path: Path) -> None: excerpts = load_jsonl(input_path) height = 1800 image = Image.new("RGB", (WIDTH * SCALE, height * SCALE), "#fbfaf7") draw = ImageDraw.Draw(image) title_font = font("georgia_bold", 54) sub_font = font("arial", 23) company_font = font("georgia_bold", 31) label_font = font("arial_bold", 16) theme_font = font("arial_bold", 15) quote_font = font("georgia", 24) footer_font = font("arial", 15) rank_font = font("arial_bold", 19) colors = { "bg": "#fbfaf7", "ink": "#151515", "muted": "#6a655e", "line": "#dedbd4", "prior": "#2f5b72", "latest": "#9c2f2f", "prior_bg": "#f1f7fa", "latest_bg": "#fff2ef", "card": "#ffffff", } def rounded(box: tuple[int, int, int, int], fill: str, outline: str | None = None, radius: int = 18) -> None: draw.rounded_rectangle( [int(value) for value in box], radius=radius * SCALE, fill=fill, outline=outline, width=SCALE if outline else 1, ) draw.rectangle([0, 0, WIDTH * SCALE, height * SCALE], fill=colors["bg"]) draw.rectangle([0, 0, 14 * SCALE, height * SCALE], fill=colors["latest"]) x0 = 78 * SCALE y = 58 * SCALE draw.text((x0, y), "Example lexical pivots from recent calls", font=title_font, fill=colors["ink"]) y += 66 * SCALE draw.text( (x0, y), "Prior-quarter language on the left. Latest-quarter language on the right.", font=sub_font, fill=colors["muted"], ) y += 58 * SCALE rounded((x0, y, x0 + 138 * SCALE, y + 36 * SCALE), colors["prior_bg"], None, 12) draw.text((x0 + 18 * SCALE, y + 10 * SCALE), "PRIOR CALL", font=label_font, fill=colors["prior"]) arrow_x = x0 + 160 * SCALE draw.line((arrow_x, y + 18 * SCALE, arrow_x + 44 * SCALE, y + 18 * SCALE), fill=colors["muted"], width=2 * SCALE) draw.polygon( [ (arrow_x + 44 * SCALE, y + 18 * SCALE), (arrow_x + 34 * SCALE, y + 11 * SCALE), (arrow_x + 34 * SCALE, y + 25 * SCALE), ], fill=colors["muted"], ) rounded((x0 + 226 * SCALE, y, x0 + 390 * SCALE, y + 36 * SCALE), colors["latest_bg"], None, 12) draw.text((x0 + 244 * SCALE, y + 10 * SCALE), "LATEST CALL", font=label_font, fill=colors["latest"]) y += 64 * SCALE card_x = x0 card_width = (WIDTH - 156) * SCALE card_height = 460 * SCALE for company in DISPLAY_ROWS: rounded((card_x, y, card_x + card_width, y + card_height), colors["card"], colors["line"], 22) cx = card_x + 28 * SCALE cy = y + 26 * SCALE rounded((cx, cy, cx + 44 * SCALE, cy + 44 * SCALE), colors["ink"], None, 10) draw.text((cx + 14 * SCALE, cy + 9 * SCALE), str(company["rank"]), font=rank_font, fill="#ffffff") draw.text( (cx + 62 * SCALE, cy + 2 * SCALE), f"{company['ticker']} - {company['display_name']}", font=company_font, fill=colors["ink"], ) cy += 62 * SCALE draw.line((cx, cy, card_x + card_width - 28 * SCALE, cy), fill=colors["line"], width=SCALE) cy += 22 * SCALE theme_width = 126 * SCALE quote_width = 520 * SCALE gap = 26 * SCALE prior_x = cx + theme_width latest_x = prior_x + quote_width + gap + 40 * SCALE for theme, finding, prior, latest in company["rows"]: row = find_excerpt(excerpts, str(company["ticker"]), finding, prior, latest) row_y = cy draw.text((cx, row_y + 27 * SCALE), theme.upper(), font=theme_font, fill=colors["muted"]) rounded((prior_x, row_y, prior_x + quote_width, row_y + 84 * SCALE), colors["prior_bg"], None, 14) rounded((latest_x, row_y, latest_x + quote_width, row_y + 84 * SCALE), colors["latest_bg"], None, 14) draw_wrapped( draw, f"\"{row['prior_excerpt']}\"", prior_x + 20 * SCALE, row_y + 20 * SCALE, quote_font, colors["prior"], quote_width - 40 * SCALE, ) draw_wrapped( draw, f"\"{row['latest_excerpt']}\"", latest_x + 20 * SCALE, row_y + 20 * SCALE, quote_font, colors["latest"], quote_width - 40 * SCALE, ) ax = prior_x + quote_width + 12 * SCALE ay = row_y + 42 * SCALE draw.line((ax, ay, ax + 28 * SCALE, ay), fill=colors["muted"], width=2 * SCALE) draw.polygon([(ax + 28 * SCALE, ay), (ax + 19 * SCALE, ay - 7 * SCALE), (ax + 19 * SCALE, ay + 7 * SCALE)], fill=colors["muted"]) cy += 106 * SCALE y += card_height + 26 * SCALE draw.line((x0, y + 8 * SCALE, (WIDTH - 78) * SCALE, y + 8 * SCALE), fill=colors["line"], width=SCALE) draw.text( (x0, y + 32 * SCALE), "Source: earnings-call transcripts. Language screen only; not investment advice.", font=footer_font, fill=colors["muted"], ) crop_height = min(height * SCALE, y + 76 * SCALE) image = image.crop((0, 0, WIDTH * SCALE, int(crop_height))) image = image.resize((WIDTH, int(crop_height / SCALE)), Image.Resampling.LANCZOS) output_path.parent.mkdir(parents=True, exist_ok=True) image.save(output_path, quality=95) def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser() parser.add_argument("--input", default="out/best_excerpt_comparisons.jsonl") parser.add_argument("--output", default="out/substack_lexical_pivots_card.png") return parser.parse_args() def main() -> int: args = parse_args() render(Path(args.input), Path(args.output)) print(args.output) return 0 if __name__ == "__main__": sys.exit(main())