An Image to ASCII Art Converter

Let’s implement a tool to make beautiful ASCII art.

The Algorithm in a Nutshell

The algorithm reads an input image in grayscale mode and translates each gray pixel into an ASCII character.

It uses an ASCII palette, namely a sequence of characters ordered by their perceived brightness from darkest to brightest:

@%?!;:. 

On a light palette the higher the typographic density the darkest the character appearance, as opposed to a dark palette:

 .:;!?%@

Pixel-to-Character Translation

Let g be a gray pixel value from 0 to 255, namely its brightness from black to white.

Let’s map g to the i-th character whose position in the palette is proportional its brightness, by computing:

  • i = (g÷255)×(P-1), where P is the palette length.

The Implementation

See the example and get the code!

This work is the result of polishing a program I implemented years ago inspired by Paul Bourke’s writing on ASCII art.


© 2024 Massimo Nazaria

RSS

Licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.