Overview
asciigen takes an input image and font, and renders the image as a block of ASCII text. It supports two modes for choosing which characters to place: a fast greedy algorithm that fills each line character-by-character, and a genetic algorithm that evolves whole lines of text toward the target image.
Features
- Converts any image to grayscale and binarizes it before matching
- Greedy mode picks, for each position, the character whose rendered glyph best matches the underlying pixels
- Genetic mode evolves a population of candidate lines over multiple generations using selection, crossover, and mutation
- Escapes Markdown-special characters in the output so results can be pasted directly into Markdown
- Optional live visualization of the matching/evolution process with Pygame
- Adjustable image scale factor via a command-line flag
How it works
The target image is loaded with Pillow and converted to a grayscale, binarized NumPy array. In greedy mode, each line is built left to right by testing candidate characters from the font and keeping whichever minimizes pixel difference against the target region. In genetic mode, a population of candidate strings per line is scored by rendering each one with Pillow and comparing it pixel-by-pixel (weighted to favor the left side of the line) against the target, then bred across generations using weighted selection, single-point crossover, and random mutation.
