[TOP TIP] Text to image notifications... with fortune & cows!

Developers are weird people, at least that is what my wife tells me all the time. Still, there are times when being weird is useful, like when you want to create a notification system that generates images based on piped text.

So lets say that we have a script which generates some text (or a lot of text) and we need to display it somewhere as an image, for example send it as an attachment, display it on a browser or even display it on your desktop as a popup.

Requirements

For fun, we are going to use an old unix tool called fortune, in most recent distros it can be installed with yum/dnf/apt. The second tool we are going to use is cowsay, which is another of those weird tools that exist and offer little real functionality but can brighten your day. Finally, we include ImageMagick, which is… pure magick!

yum install fortune-mod cowsay ImageMagick

Generate some text

Lets generate some random fortune, and to make the output extra weird we pipe it to cowsay:

$ fortune | cowsay
 _________________________________________
/ Cats are smarter than dogs. You can't   \
| make eight cats pull a sled through the |
\ snow.                                   /
 -----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

In a real life scenario, our script would probably generate real information (like server status data, server security warnings, that sort of thing). But for our purposes, lets look at these two commands.

The fortune command reads quotes from a ton of files under /usr/share/games/fortune and chooses one at random. You may see all the available categories of quotes by running "fortune -f", then limit the generated quotes to a specific category with "fortune bofh-excuses" or even choose from the "short" or "long" quotes with "fortune -s" and "fortune -l" respectively.

The cowsay command takes any input and redisplays it again but as a ascii quote from a cow, or a kitty, or a koala, or a tux, and many other ascii art designs. You may see all the available ascii art designs with "cowsay -l" and select one with "cowsay -f tux" or even change the word-wrap length based on your terminal size with "cowsay -W 80".

Text to image

As I mentioned above, ImageMagick is magick! We may use the convert command to pipe text and generate images, for example:

fortune -s | cowsay | convert -background lightblue -fill blue -font Liberation-Mono-Bold -size '800x600' -trim label:@- cowsay.png

The above convert command is explained as:

-background lightblue -fill blue = Light blue background with normal blue writing.

-font Liberation-Mono-Bold = Choose a monospaced font, so that the ascii art draws up properly. If you don't have this particular font, choose another one by looking at the output of this command: convert -list font.

-size '800x600' -trim = Starting size of the image but trim to the size of the generated text.

label:@- = Read piped input.

cowsay.png = Export to image file.

We may then proceed to use the generated image for our needs.

~~![](<URL url=)https://i.imgur.com/Vp7To7K.png" />

Enjoy fortune & cows!~~

0 Replies

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct