August 7, 2006

CLI Magic: Make instant Web banners from the command line

Author: Michael Stutz

Sometimes you might have a need to make an image file consisting mostly of text -- to use as a banner on a Web page, for instance. You can always make the banner in an image editor, but there's a much quicker way -- one that lends itself to automatic generation from scripts using arbitrary text.

GNU Enscript is a tool that converts text to PostScript, and when combined with the ImageMagick suite of image processing tools -- which comes installed as standard equipment on almost all Linux distributions these days -- you can build nice-looking PNG and JPEG image banners for Web sites, on the fly, from any text you give it.

What you do is send the text to put in the banner to Enscript, which outputs a PostScript page containing that text set in a font of your choosing. Then use ImageMagick's convert tool to turn that PostScript page into a cropped image file, optionally touched up with a few effects and enhancements. Here's how it's done.

Make PostScript from your input text

First, you want to pick your font. You'll find a default list of available PostScript fonts in the file /usr/share/enscript/afm/font.map; specify the full font name as it appears in that file followed immediately with the point size you want, as a quoted argument to enscript's -f option. So if you want to use Palatino Bold at 48 points, you'd use Palatino-Bold48 as the argument.

You'll also want to give the -B option, which disables all default headers. Write the output to a file by giving its name as an argument to the -o option.

Finally, give the actual banner text by piping the output of echo to the enscript command. For example:

$ echo "The blogroll" | enscript -B -f "Palatino-Bold48" -o blog.ps

This makes a new file, blog.ps, which is a letter-sized PostScript page containing the text "The blogroll" in Palatino Bold at 48 points.

That's nice, but there isn't much you can do with it -- it's still far from a banner you can put on your site. You'll now have to transform the PostScript into something usable.

Convert the PostScript to an image

The convert tool will take the PostScript and write a new file in a usable image format of your choosing, such as JPEG or PNG.

But you don't want a big page-sized image, you want a small banner. That's where the -crop option comes into play -- it'll cut out all the extra white space in the image. Use "0x0" as an argument to automatically trim the image right around the text:

$ convert -crop 0x0 blog.ps blog.jpeg

You'll have a new file, blog.jpeg, that looks much more like a banner now -- see illustration 1.

Enhance the image

The default banner style is plain and basic -- good for some purposes, but you might want to jazz it up. Here are some enhancements you can make to the basic design (and there's a lot more that you can do than what's described here -- browse the many options listed in the convertman page to get an idea).

You can inverse the colors, making white text on a black background, using the -negate option.

If you want a border around the banner, use convert's -border option, giving an argument of "WIDTHxHEIGHT" (in pixels); to make it color (it defaults to gray), specify the name of the color as an argument to the -bordercolor option. You can use actual X server color names ("Red", "SeaGreen") or RGB values ("#ff0000", "#2e8b57"). For example, this command makes a cropped PNG image of your PostScript input file with white text on a black background, adding a blue border 1 pixel wide and 1 pixel high:

$ convert -crop 0x0 -negate -border 1x1 -bordercolor blue blog.ps blog.png

The PNG image file, blog.png, will look like illustration 2.

Do it on the fly

Of course you can combine all of this together and do it in one step -- which is useful for making buttons or banners on-the-fly, such as in scripts.

Do it by sending the output from enscript to the standard output by using - as an argument to the -o option. Then, use convert further down the pipeline, giving - as the name of the input file, so that it takes standard input.

For instance, you can make a PNG image of the text "Headlines for" followed by the name of the month and the date, set in the Times Roman Bold typeface at 48 points, with this one-liner:

$ echo "Headlines for `date +'%B %e'`" | enscript -o - -B -f "Times-Bold48" | convert -crop 0x0 - headlines.png

Click Here!