Rendering images as text
Text Art
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
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.