Linux.com

Feature

CLI Magic: Make instant Web banners from the command line

By Michael Stutz on August 07, 2006 (8:00:00 AM)

Share    Print    Comments   

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

Illustration 1 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 convert man 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

Illustration 2 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

Share    Print    Comments   

Comments

on CLI Magic: Make instant Web banners from the command line

Note: Comments are owned by the poster. We are not responsible for their content.

Re:cropping

Posted by: Anonymous Coward on August 07, 2006 10:33 PM

The command is not correct for cropping. Try this:

convert -crop WxH+0+0 filename.ps filename.jpg

...where W is the width you want to end up with, and H is the height. The '+0+0' is the offset, the starting point for the crop (upper left corner of the image). If you don't include an offset you get a series of 'tile images', each the size of the WxH.

If you follow the article and use '-crop 0x0', nothing is cropped.

#

Re:cropping

Posted by: Administrator on August 08, 2006 04:31 AM
Thanks for the addition. I was wondering about that. The one thing I hate more than banners is when things don't work...

#

My belief

Posted by: Anonymous Coward on August 08, 2006 02:55 AM
My belief is that banners are the devil!

btw, the example use<nobr> <wbr></nobr>.jpeg in one of its examples, use<nobr> <wbr></nobr>.png instead, JPEG is more suited for photographies while PNG is more suited for banners and web graphics.

#

Re:cropping

Posted by: Anonymous Coward on August 08, 2006 04:30 PM
Three small corrections.



The right way to crop the image to just fit the text is to use <tt>-trim +repage</tt> in ImageMagick.



Also, the <tt>-bordercolor</tt> directive must come before the <tt>-border</tt> directive, and not as shown in the article.



Lastly, ImageMagick has not had a <tt>man page</tt> since about version 6.2.2, at least in my installation.

The old content is now installed as html under<nobr> <wbr></nobr><tt>/usr/share/doc/imagemagick...</tt>,
and is easier to use.



Since these methods require knowledge of ImageMagick, one may as well do the whole job in ImageMagick.

There are many examples in <a href="http://www.cit.gu.edu.au/~anthony/graphics/imagick6/text/" title="gu.edu.au">http://www.cit.gu.edu.au/~anthony/graphics/imagic<nobr>k<wbr></nobr> 6/text/</a gu.edu.au>.



Still, it was good to learn about <tt>enscript</tt>.


JG

#

Something simpler

Posted by: Anonymous Coward on August 10, 2006 01:11 AM
Simpler one-line command:
convert -font Helvetica-Bold -pointsize 48 -background black -fill red label:'Hallo world' allo.png

#

cropping

Posted by: Administrator on August 07, 2006 09:53 PM
When I tried those commands, I didn't get a banner-style image, but a full page image with the text at the top... Is there a requirement in ImageMagick version for this to work?

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya