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
Direct Download
Read full article as PDF:
Price $2.95
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
News
-
KDE Plasma 6 Looks to Bring Basic HDR Support
The KWin piece of KDE Plasma now has HDR support and color management geared for the 6.0 release.
-
Bodhi Linux 7.0 Beta Ready for Testing
The latest iteration of the Bohdi Linux distribution is now available for those who want to experience what's in store and for testing purposes.
-
Changes Coming to Ubuntu PPA Usage
The way you manage Personal Package Archives will be changing with the release of Ubuntu 23.10.
-
AlmaLinux 9.2 Now Available for Download
AlmaLinux has been released and provides a free alternative to upstream Red Hat Enterprise Linux.
-
An Immutable Version of Fedora Is Under Consideration
For anyone who's a fan of using immutable versions of Linux, the Fedora team is currently considering adding a new spin called Fedora Onyx.
-
New Release of Br OS Includes ChatGPT Integration
Br OS 23.04 is now available and is geared specifically toward web content creation.
-
Command-Line Only Peropesis 2.1 Available Now
The latest iteration of Peropesis has been released with plenty of updates and introduces new software development tools.
-
TUXEDO Computers Announces InfinityBook Pro 14
With the new generation of their popular InfinityBook Pro 14, TUXEDO upgrades its ultra-mobile, powerful business laptop with some impressive specs.
-
Linux Kernel 6.3 Release Includes Interesting Features
Although it's not a Long Term Release candidate, Linux 6.3 includes features that will benefit end users.
-
Arch-Based blendOS Features Cool Trick
If you're looking for a Linux distribution that blends Linux, Android, and web apps together, blendOS might be what you're looking for.