Rendering images as text

On the Web

If you're familiar with HTML code, you will have seen the HTML <img> tag. The code in Listing 1 references the tux.png graphics file and embeds it for display at the current location on a webpage.

Listing 1

Including an Image in HTML

<img src="tux.png" alt="Tux, the Linux penguin">

The alt attribute (which has been mandatory since 2011) specifies an image's alternative text that the web browser will display if the image cannot (or is not supposed to) load. For instance, if you view the website with a screen reader such as Orca [13], it will read out the alternative text.

The aha ANSI HTML adapter, img2txt, and jp2a (by specifying appropriate options) convert text with ANSI color codes (or images) into text-based HTML sequences and HTML tags for setting the color. You can then copy this output directly into your HTML file (Figure 4).

Figure 4: Tux as a text image on a web page.

Why would you want to use text images on the web? You might want to integrate terminal content as a screenshot on a web page without having to use an image, saving data – this way the screenshot is more or less in the original format, namely text.

Figure 5 shows a directory listing with special pink background. Using the command sequence in Listing 2, you pipe the output of the ls command to aha, which gives it a pink background and outputs it to the ls.html file.

Listing 2

HTML with Aha

$ ls --color=always | aha --pink > ls.html
Figure 5: Terminal output as an image in the web browser.

Theoretically, these results can also be read by a screen reader. In testing, however, this currently only works reliably with black and white images. Presumably, the contrast is not high enough for the screen reader if you use other color combinations.

Videos

You can also use these techniques with videos. To do this, you use AAlib and libcaca as video output plugins for MPlayer and VLC (both), xine (AAlib), and mpv [14] (libcaca). The Hasciicam project [15] renders images from a TV card or camera as an ASCII image.

The mpv player also comes with its own format, True Color Text (TCT). However, TCT's documentation is quite sparse; it is only mentioned in mpv's help page. (In fact, we were only able to conclude from comments in the source code the meaning of the TCT acronym.) More information on the subject would be useful.

Table 2 shows some sample commands for playing movie sequences; Figure 6 shows the matching output using aaxine.

Table 2

Converting Videos with AAlib and libcaca

Program

Sample Command

mpv with TCT library

mpv --vo=tct https://youtu.be/Qd_1t7kw5EA

mpv with libcaca

mpv --vo=caca https://youtu.be/Qd_1t7kw5EA

MPlayer with AAlib

mplayer -vo aa video.mp4

xine with AAlib

aaxine video.mp4

Figure 6: Using aaxine to play a video.

The whole thing works quite well, but it does reach its limits if you need to view recorded keynote presentations, for example. If slides are embedded in the presentation recording, the text on the slides is often unreadable. To read these slides, you would need OCR capabilities in addition to the ability to reduce the resolution and convert to a different format.

QR Codes

What works with images and videos also works with QR codes. Put simply, QR codes are actually just special images that can be displayed at the command line. The advantage here is that you don't have to render the QR code's rough "pixels" as glyphs. You simply need to convert each of the blocks into an empty space or a half or whole Unicode block character.

This means that QR codes look just as crisp on a text-based terminal as they do as real images. The same applies to the text representation on web pages. Helpful tools for doing this include qrencode [16] and qrcode [17] from the Debian go-qrcode package. Figure 7 shows the foobar string as a QR code in text form.

Figure 7: The foobar string as a QR code in text form.

A possible application for this is provided by the pass-otp one-time password plugin [18] in the pass [19] password manager. With the help of qrencode and qrcode, secrets transferred via QR code can also be output as QR codes – even on a text-based terminal.

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.

  • FOSSPicks

    After vowing to not be impressed by AI-generated images, Graham has spent this month communing with Stable Diffusion like it's some locally installed oracle or prophet of truth.

  • 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.

comments powered by Disqus
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.

Learn More

News