2017-07-15, post № 175
art, ascii, programming, Python PIL, #ANSI, #ASCII, #characters, #image processing, #low resolution, #retro look
Most images nowadays are represented using pixels. They are square, often relatively small and numerous, come in different colors and thereby do a good job being the fundamental building block of images. But one can imagine more coarse-grained and differently shaped pixels.
An interesting fact is, that in most monotype fonts two characters placed right next to each other (for example ‘
$$’) occupy roughly a square area. So simple ASCII characters can indeed be used to approximately describe any ordinary image.
Asciify does exactly this; it takes in an image and some optional parameters and maps the pixels’ intensity onto a character set. Both the large and small default character sets are taken from a post by Paul Bourke.
In conjunction with
asciify.py, I wrote
index.py, which asciifies a bunch of images and results in their html form; it also creates an index. All images asciified for this post can be viewed through this index .
Converting an image to its asciified form works best when there is a lot of contrast in the image. Because of this, some pre-processing of the image may be required for best results (all images shown where only cropped or rotated). The built-in color functionality also only knows of 𝟪 colors, so bright and different colors look the best, as they interestingly differentiate from one another. The asciified image’s size also plays a role, the larger it is, the better the characters blend into one and appear to be one image.
Asciify is operated on a command prompt;
python asciify.py img.png. To parse arguments, the built-in Python module
argparse is used. The images are opened and read using the Python Imaging Library module
PIL, which needs to be installed for this program to work.
Optional arguments include
--size N, where the maximum size can be specified,
--smallcharset, which can sometimes increase the asciified image’s visual appeal and
--html, which will output an html file to be viewed in a browser. To see the program’s full potential, simply run
python asciify.py --help .
Source code for both asciify.py and index.py can be downloaded, the first is also listed below.
The two examples above use the color mode, though certain images also work in default black and white mode, such as this spider I photographed.
Then again, the colored text also has its charm, especially when the source image has bright colors and a lot of contrast.