Rendering images as text

Text Art

© Lead Image © anan punyod, 123RF.com

© Lead Image © anan punyod, 123RF.com

Article from Issue 245/2021
Author(s): , Author(s):

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.

Figure 1: An excerpt of tux.txt shows a portion of the Tux logo in the form of ANSI control codes.

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

ANSI with color codes

bbfr

BBCode [6]

caca

Internal libcaca format

html

HTML with support for CSS and DIV

html3

HTML with tables

irc

IRC with CTRL+K control codes

ps

PostScript

svg

Scalable Vector Graphics (SVG)

tga

Targa image format

utf8

UTF-8 with carriage return

utf8cr

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
Figure 2: Using chafa to show Tux as a text image.

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.

Figure 3: A text image of Linus Torvalds constrained to a width of 100 pixels with catimg.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • ASCII Art

    Creating images from letters and numbers is a complex matter, unless you have the right tools at hand.

  • Command Line

    This month, we’ll show you how to decorate your console and give it a fresh new look by adding color to the command line with ANSI escape sequences.

  • Harness the Web from the Command Line

    Not only is it possible to browse the web from the CLI, it's mightily useful, too, as we demonstrate in this article.

  • CLI Image Processing

    Powerful command-line tools offer fast and easy image editing.

  • ImageMagick

    GIMP isn’t the only option for photo manipulation. ImageMagick, a collection of command-line programs for image processing, can help you process multiple images in one go.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News