Rendering images as text
Text Art

© Lead Image © anan punyod, 123RF.com
If you need to display an image in the terminal or as plain HTML, a variety of smart tools can help with the conversion.
Thanks to increasingly sophisticated technology, displaying high-resolution images on screen is no longer a difficult task. However, these more detailed images (with a combination of greater image size, resolution, and color depth) come at a cost, consuming more storage space and taking longer to download from remote sources, such as web browsers and webcams.
Sometimes you just need an image to display quickly. You can save time and bandwidth by displaying images at a lower resolution and color depth as text (ASCII or Unicode characters) directly in the terminal and converting them with American National Standards (ANSI) color codes [1]. You can also convert the images to plain HTML and CSS and embed the results in a web page. Some text browsers, such as ELinks, then display these images directly in the accessed web page. In a similar manner, the Browsh [2] browser does this internally and can also render images as text.
In this article, we will discuss the available tools for converting images to text and explore whether this approach is suitable for everyday use. This article follows up on a previously published article [3] that dealt with tools for creating ASCII art.
The Conversion
First, you need to convert an image into individual characters before embedding or displaying it. To do this, each pixel (or group of pixels) is assigned a suitably colored single letter, or more precisely a glyph (a graphic symbol).
You do the conversion via filters in the form of libraries. For example, you can use the aview
and asciiview
tools from the Ascii Art Library (AAlib) [4] or img2txt
and cacaview
from the Colour Ascii Art Library (libcaca) [5]. The end result is a rendering of the image using letters with ANSI control codes. The resulting reduction in resolution and color depth not only reduces the volume of data to be transmitted, but it also means that the converted image can be displayed in a text terminal or text-based browser.
As an example, you can convert a PNG image of the Tux logo into letters with libcaca's img2txt
(which replaces imgtoppm
from previous library publications). To create a letter file named tux.txt
, you use the following command:
$ img2txt tux.png > tux.txt
Figure 1 shows an excerpt from the content of the tux.txt
file. Without additional switches in the call, the output text is 60 characters wide and formatted as colored ANSI.
You can use the -W
(--width
) option to regulate the width of the output. If you do not specify anything using -H
(--height
), img2txt
scales the height of the output to match the original aspect ratio. The -f
(--format
) option lets you define the output format. Table 1 summarizes img2txt
's output options.
Table 1
img2txt Output Options
Option | Description |
---|---|
|
ANSI with color codes |
|
BBCode [6] |
|
Internal libcaca format |
|
HTML with support for CSS and DIV |
|
HTML with tables |
|
IRC with CTRL+K control codes |
|
PostScript |
|
Scalable Vector Graphics (SVG) |
|
Targa image format |
|
UTF-8 with carriage return |
|
UTF-8 with carriage return and line feed |
If you want to save ANSI images as normal images again, you can use the AnsiLove library [7]. By interpreting the ANSI codes, AnsiLove creates screenshots in PNG format, rather than forcing you to create a screenshot of the text terminal.
Displaying Converted Images
For quite some time now, we have been experimenting with tools for displaying the converted images. In the Spring of 2020, Axel's toolbox [8] included chafa
[9] and aha
[10], along with the catimg
[11] utility. Although the name is definitely a reference to the cat
/tac
commands, you might interpret catimg
to be a tool for displaying cat pictures, but it can do considerably more. During our research, jp2a [12], which works similarly to img2txt
, also appeared on the scene.
Functioning as image viewers for the terminal, chafa
and catimg
have only been an integral part of a stable release of the Linux distribution since Debian GNU/Linux 10. chafa
displays one or more images as an unabridged slideshow in the terminal (Figure 2). It scales an image to match the current width and height of the terminal window. On the other hand, catimg
orients an image based on its width resulting in the upper edge of the image disappearing from the terminal display during scrolling. The following two calls use chafa
to show a single image and a slideshow of all PNG files in the current directory:
$ chafa linux.png $ chafa *.png
chafa
comes with a number of interesting options for effects. For example, you use -c
(--colors
) to set color mode to 2, 16, or 256 colors or to a 24-bit view mode. The -d
(--duration
) option determines how long an image remains in the slideshow (the default is three seconds). You can define the output size with -s
(--size
) <width>x<height>
. By default, chafa
uses the terminal's size or 80x25 characters if it cannot determine the size. The --watch
option outputs the image again for each change. With catimg
, you can use the -l
option to control how often it plays an animated GIF.
Neither chafa
nor catimg
can handle crossfade effects to make the slideshows more interesting. Perhaps the developers will read this article and consider adding such a feature in the future.
Differences
For all their similarities, the tools presented in this article have distinct peculiarities. For example, aview
can only handle images in PNM format and only display images in grayscale since AAlib does not support colors. AAlib's asciiview
functions as a wrapper around aview
that converts images to the PNM format required by aview
up front using external tools. Animated GIFs do not work.
Unless you specify otherwise in the call, img2txt
converts images to ASCII instead of Unicode glyphs and uses only 16 colors. Depending on the terminal, some characters might flash. Working similar to img2txt
, cacaview
is a plain vanilla image viewer, which opens a separate window to use the best possible terminal settings. As a result, nothing flashes here. However, animated GIFs do not work here either.
Both chafa
and catimg
display PNGs, JPEGs, GIFs, and many other image formats in more than 16 colors. If the terminal supports it, both can also use Unicode glyphs. Both can also handle animated GIFs. When displaying animated GIFs, the programs display the images in an endless loop instead of exiting by themselves.
Since catimg
always uses the whole terminal width for display, you can use the -w
option to specify the width of the output if necessary. Figure 3 shows this for a width of 100 pixels.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
Fedora 39 Beta is Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.
-
UbuntuDDE 23.04 Now Available
A new version of the UbuntuDDE remix has finally arrived with all the updates from the Deepin desktop and everything that comes with the Ubuntu 23.04 base.
-
Star Labs Reveals a New Surface-Like Linux Tablet
If you've ever wanted a tablet that rivals the MS Surface, you're in luck as Star Labs has created such a device.
-
SUSE Going Private (Again)
The company behind SUSE Linux Enterprise, Rancher, and NeuVector recently announced that Marcel LUX III SARL (Marcel), its majority shareholder, intends to delist it from the Frankfurt Stock Exchange by way of a merger.