Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells, Featured

Convert Color Text to Images in Bash

Submitted by on December 11, 2021 – 12:14 pm

The textmg is a cool little CLI tool written by jiro4989 – a systems engineer from Japan – that allows you to convert the color output in a terminal window to an image file. I find this very useful when sharing code snippets on social media and when writing documentation.

Installation

Debian-based distros

u="https://github.com/jiro4989/textimg/releases"
v="$(curl -s0 -k "${u}" | grep -oP "(?<=/v)[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}(?=/)" | sort -Vu | tail -1)"
wget "${u}/download/v${v}/textimg_${v}_amd64.deb"
sudo dpkg -i ./textimg_${v}_amd64.deb

RHEL-compatible distros

u="https://github.com/jiro4989/textimg/releases"
v="$(curl -s0 -k "${u}" | grep -oP "(?<=/v)[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}(?=/)" | sort -Vu | tail -1)"
sudo yum install "${u}/download/v${v}/textimg-${v}-1.el7.x86_64.rpm"

Examples

Example 1Example 2Example 3Example 4

Colorize output of top using lolcat and save it as a PNG file:

top -b -n 1 | lolcat | textimg -o /var/tmp/test_${RANDOM}.png

See the output

Save output of screenfetch (see it here) as a graphic file:

screenfetch | textimg -o /var/tmp/test_${RANDOM}.png

See the output

Convert a shell script with syntax highlighting to PNG using the syntax function (you can find it here):

syntax atq_jobs.sh | textimg -o /var/tmp/test_${RANDOM}.png

See the output

Using the fact function (you can find it here), generate fifty PNG images:

for i in `seq 1 50`; do fact | textimg -o /var/tmp/test_${RANDOM}.png; done

See the output

The 'fact' function

#/*       _\|/_
#         (o o)
# +----oOO-{_}-OOo-------------------------------------------------------------+
# |Show a random fact                                                          |
# |                                                                            |
# |Installation:                                                               |
# |-------------                                                               |
# |                                                                            |
# |Install optinal `boxes` and `coreutils` packages:                           |
# |(apt|yum|dnf) install boxes coreutils                                       |
# |                                                                            |
# |Install optional `lolcat` package:                                          |
# |pip install lolcat                                                          |
# +---------------------------------------------------------------------------*/


fact() {
  factx() {
    wget randomfunfacts.com -O - 2>/dev/null | \
    grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;;"
  }
  if hash fmt boxes lolcat 2>/dev/null; then
    factx | fmt -s -w 48 | boxes -d peek -a l -s 79 | lolcat
  elif hash fmt boxes 2>/dev/null; then
    factx | fmt -s -w 48 | boxes -d peek -a l -s 79
  elif hash fmt 2>/dev/null; then
    factx | fmt -s -w 48
  else
    factx
  fi
}

Print Friendly, PDF & Email

Leave a Reply