Automated OG Image Generation: Rust + SVG → WebP
For My Second Brain I create programmatical way of generating
OG (Open Graph) / hero image. These get’s generated by adding the title of the note to the SVG, but otherwise use a og_template.svg as a template, then converts them to WebP format for optimal web performance.
GitHub and Code
Everything is available under my public second brain repo on GitHub: second-brain-public)
# Log
# 2026-04-17
- I updated and added image description to the image
- and I just learned about
Twitter:cardlarge image and small!- check Twitter
# SVG -> WebP Generation Steps
All the code is on my public second brain repo.
-
SVG Template Preparation
- Create any base SVG (I use 1200x628px) with a black background
- Add visual aesthetics. I added hexagonal network pattern, grid, and glowing center in gold (#D4AF37)
- Set fixed header and footer. I used
SECOND BRAINand footerA Digital Vault of Knowledge
-
Dynamic Title Processing
- Split title into balanced lines (~20 chars each)
- Auto-adjust font size: 96px (1-2 lines), 84px (3 lines), 72px (4+ lines)
- Center text between header and footer
-
Image Conversion
- Generate SVG with text overlay
- Convert to WebP using ImageMagick:
magick input.svg output.webp - Images are stored in
static/feature/genfolder - Skip if Image already exists
-
Markdown Integration
- This is my Frontmatter in my notes if I want to statically define an image. If no image defined, it will use the generated one.
1 2 3 4 5 6--- title: "Your Title" ogimage: "generated-image-name.webp" ogwidth: 1200 ogheight: 628 ---
Generate Features Images (og-image) of Notes
# Other Resources
- OG image for mdBook, my implementation in this PR. More details on mdBook
- shot-scraper: A command-line utility for taking automated screenshots of websites
- How to Dynamically Create Open Graph Images with Cloudinary and Next.js - Braydon Coyer
- An code example with Astro: webpro.nl/src/pages/og/[…route].ts at main · webpro/webpro.nl · GitHub
- Generating SVG with Recraft V3
# Detailed Explanation
Below explains how to generate social media preview images (Open Graph images) programmatically using Rust and SVG templates. It’s integrated into my Hugo static site generator workflow (see Makefile) and automatically creates unique images for each published note.
# What This Does
When you tag a note with #publish in your Obsidian vault:
- The note is copied to the
content/directory - A custom OG (Open Graph) image is generated from the note’s title
- The image is saved as a WebP file for web optimization
- The frontmatter is updated with the image path
- Hugo uses this image for social media previews
# Why Use Rust + SVG?
Rust advantages: Fast performance for batch processing hundreds of notes. I used Python initially for parts, but it was very slow for so many notes
SVG advantages: Easy text manipulation and dynamic content and small file sizes before conversion
# Technical Architecture
# Components
- SVG Template (
og_template.svg): A reusable design template with a placeholder for dynamic content - Rust Generator (
svg_generator.rs): Code that processes titles and injects them into the template - ImageMagick: Converts SVG to WebP format for web delivery
- Makefile Integration: Automated workflow for building and deploying
# Design Specifications
- Dimensions: 1200 × 630px (standard Open Graph format)
- Aspect Ratio: ~2:1 (optimized for social media platforms)
- Format: SVG → WebP conversion
- Color: My Websites Color Palette
# How It Works
- SVG Template Structure
- Title Processing Logic
- Dynamic Font Sizing Font: size adapts to title length for optimal readability:
- SVG Generation Process
- Image Generation Workflow
# Usage Guide
Prerequisites:
|
|
# GitHub Code
- Rust code: svg_generator.rs
- Documentation: SVG-design-integration.md
# Further Reads
- Prompts for image generation in the first place: My AI Prompts
- Dynamic Feature Images
Origin:
X
References: My Second Brain