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
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.