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
}