How sanster/text_renderer works
Project type and purpose
• This is a standalone CLI tool (“Text Renderer”) for generating synthetic text images (both Latin and non-Latin) to train OCR models. It reads text corpora and font data, applies layout and visual effects, and writes out image files plus labels.
High-level architecture overview
• The system is organized around a single main entry point (main.py) that wires together:
• An argument/config loader
• A data layer (fonts, char lists, corpora)
• A modular text-generation pipeline (corpus selection → line creation → effects → rendering)
• An output writer
• Optional GPU acceleration is provided via a compiled Cython/OpenCV module.
Main components and their relationships
A. CLI & Configuration
• parse_args.py / help_runner.py
• libs/config.py reads YAML (configs/default.yaml or user-supplied)
→ produces a Config object used by downstream modules
B. Data Layer (read-only resources)
• data/chars (character files)
• data/corpus (raw text files)
• data/fonts (directory trees for each language)
• data/fonts_list (precomputed font lists)
C. Core Pipeline (under textrenderer/)
1) Corpus module (textrenderer/corpus/*.py)
• Chooses/generates text strings based on language or list rules
2) Remapper (textrenderer/remaper.py)
• Applies strict-mode filtering or character remapping
3) Liner (textrenderer/liner.py)
• Builds text lines or paragraphs, handling spacing, wrap, alignment
4) Noiser (textrenderer/noiser.py)
5) Renderer (textrenderer/renderer.py)
• Renders final image: either via PIL/OpenCV CPU path or GPU path
D. GPU Acceleration (optional)
• libs/gpu/GpuWrapper.pyx + setup.py
• Requires prebuilt OpenCV with CUDA support
• Renderer selects GPU path when “--gpu” is passed
E. Output Writer
• main.py writes images + labels to output/<config_name>/
F. Utility Scripts & Tools (separate helpers)
• tools/*.py (tfrecord maker, font checker, balancer)
• gists/*.py (small demo scripts)
Architectural patterns and principles
• Pipeline pattern: a linear sequence of text/image transformations
• Modular separation: each effect or data source lives in its own module
• Configuration-driven: effects and fractions are defined in YAML
• Optional acceleration: pluggable GPU wrapper
• CLI & Config Loader
• Data Store (Chars, Corpora, Fonts)
• Corpus Generator
• Remapper
• Liner
• Noiser
• Renderer (CPU/OpenCV & GPU/CUDA)
• Output Writer
• (Optional) Tools & Gists
• CLI → Config Loader → Core Pipeline
• Core Pipeline stages in sequence (Corpus → Remap → Liner → Noiser → Renderer)
• Renderer → Output Writer → File System
• Parser: argparse (parse_args.py)
• Config: PyYAML (requirements.txt)
• Pipeline: Python modules under textrenderer/
• Rendering: PIL, OpenCV (cv2), optional Cython/C++ GPU wrapper
• Build: setup.py for libs/gpu
d. Data flows:
• Read-only from data/ → pipeline
• Write-only to output/