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).
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
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 with libcaca |
|
MPlayer with AAlib |
|
xine with AAlib |
|
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.
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.
« Previous 1 2 3 Next »
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
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
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.