#!/usr/bin/env python3 """Render a terminal session (text file) as a dark-themed PNG screenshot. Usage: render_shot.py The session file's first line should be "$ <command>"; remaining lines are the command's output. Long lines are truncated, long sessions cut. """ import sys from PIL import Image, ImageDraw, ImageFont FONT = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf" FONTB = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf" SIZE = 15 PAD = 18 MAXCOLS = 100 MAXLINES = 42 BG = (13, 17, 23) HEADER_BG = (22, 27, 34) TITLE = (88, 166, 255) PROMPT = (63, 185, 80) CMD = (255, 255, 255) OUT = (201, 209, 217) def main(): txt_path, png_path, title = sys.argv[1], sys.argv[2], sys.argv[3] with open(txt_path, encoding="utf-8", errors="replace") as f: lines = f.read().splitlines() lines = [ln[:MAXCOLS] for ln in lines[:MAXLINES]] font = ImageFont.truetype(FONT, SIZE) bold = ImageFont.truetype(FONTB, SIZE) cw = font.getbbox("M")[2] lh = SIZE + 7 maxlen = max((len(ln) for ln in lines), default=0) width = PAD * 2 + max(maxlen, 40) * cw width = min(width, PAD * 2 + MAXCOLS * cw) header_h = 34 height = header_h + PAD + len(lines) * lh + PAD img = Image.new("RGB", (width, height), BG) d = ImageDraw.Draw(img) d.rectangle([0, 0, width, header_h], fill=HEADER_BG) for i, c in enumerate([(255, 95, 86), (255, 189, 46), (39, 201, 63)]): x = PAD + i * 18 d.ellipse([x, 12, x + 11, 23], fill=c) d.text((PAD + 62, 9), title, font=bold, fill=TITLE) y = header_h + PAD for ln in lines: if ln.startswith("$ "): d.text((PAD, y), "$ ", font=bold, fill=PROMPT) d.text((PAD + 2 * cw, y), ln[2:], font=bold, fill=CMD) else: d.text((PAD, y), ln, font=font, fill=OUT) y += lh img.save(png_path) print(png_path) if __name__ == "__main__": main()