Press no key to discontinue...

Image Magick Banner Generator - part 1

Date/Time Permalink: 03/17/08 09:08:34 am
Category: Graphics Tutorials

I'm going to try an experiment: blogging a whole project for a little graphics toy I've managed to hack out. You are welcome to copy and save scripts as we go along, just read them and learn how to do it yourself, modify them to your own purposes, or just nod your head and go "Uh-huh, gawrsh, lookit the purty code boxes!" as they fly by.

This will be a multi-part posting of a project. It's just a group of scripts which, when assembled together, make an interesting toy, and also teaches some practical use of the fantastic, but mysterious (to many), Image Magick tool suite.

If you don't want to collect the parts day by day, wait to the end when I'll wrap the whole project up in a tarball for downloading.

The first piece is a script version of a command I shared a couple days ago in Obscure Linux Commands: Some of My Favorite Incantations:

#!/bin/bash

if [ "$1" ]; then

  WORDLINE=$((($RANDOM * $RANDOM) % $(wc -l $1 | awk '{print $1}') + 1))"p"
  sed -n $WORDLINE $1

else
  echo "USAGE: random_line.sh $FILE"
fi

exit 0

Save it as "random_line.sh" if you're following along with this project. What it does is cough up a random line from any text file, and we'll be using it to select random colors from rgb.txt, random fonts from the X11 font directories, and all kinds of other abuse.

If you visit that post in the above link, be sure to check out the comments section for Michael Johnson, who posts an even more elegant construction for getting a random line from a file. As they say in the Perl community, "My apologies for the needlessly verbose script"! You can use his method or mine, no difference.

NOTE: In spite of the Unix words list being so standard that it's written up Wikipedia, not all Linux distros have it under the same name and in the same place. So check that Wikipedia article for other clues to tracking it down on your system.

Now for the second piece:

#!/bin/bash

  BEVELWIDTH=$(($RANDOM % 10 + 3))
  BEVELWIDTH=$BEVELWIDTH"x"$BEVELWIDTH

  if [ "$(($RANDOM % 2))" = 1 ]; then
    convert $1 -raise $BEVELWIDTH $1
  else
    convert $1 +raise $BEVELWIDTH $1
  fi

exit 0

Save it as "bevel.sh" if you're following with this project. All it does is add a random bevel to a given image. Given the following image saved as "solid_background_1.png":

solid_background_1.png

running "./bevel.sh solid_background_1.png" might produce this:

beveled_background_1.png

In ImageMagick's 'convert' command, "-raise" will produce a raised bevel and "+raise" will produce a sunken bevel.

And here's one more piece:

#!/bin/bash

  FRAMECOLOR=$(./random_line.sh /usr/X11R6/lib/X11/rgb.txt | cut -f 3- | sed 's/[ \t]*//g')
  BEVELWIDTH1=$(($RANDOM % 10))
  BEVELWIDTH2=$(($RANDOM % 10))
  
  CONVERTSTRING="$1 -mattecolor ""$FRAMECOLOR"
  CONVERTSTRING="$CONVERTSTRING"" -frame "$(($RANDOM % 15 + $BEVELWIDTH1 + $BEVELWIDTH2))
  CONVERTSTRING="$CONVERTSTRING""x"$(($RANDOM % 15 + $BEVELWIDTH1 + $BEVELWIDTH2))
  CONVERTSTRING="$CONVERTSTRING""+"$BEVELWIDTH1"+"$BEVELWIDTH2" "$1

  convert $CONVERTSTRING

exit 0

which you can save as "frame.sh" if you're following along. All it does is generate a random frame, which is actually kind of a double-bevel, except that it also makes the frame a random color.

Given the following image saved as "solid_background_1.png":

solid_background_1.png

running "./frame.sh solid_background_1.png" might give you this:

framed_background_1.png

You can see already we're calling "random_line.sh" to get a random color from X11's "rgb.txt" file. We also have some bizarre incantations which need explaining:

First off, the formatting on rgb.txt is a little weird. It has spaces in some color names, it uses tabs, and generally makes itself a pain to process through a script. An alternative way to get a color list is to run this command:

convert -list color

...and save the output into a file, which you can then pick a random line from.

Also, the numbers that you feed to the convert command's "-frame" switch requires that the frame width be equal to or greater than the width of the inside and outside bevels, or it will produce an error. So we have to figure that into the formula for the argument to "-frame".

Here's a tutorial on ImageMagick framing which explains it better.

OK, so far we have "random_line.sh", "bevel.sh", and "frame.sh", the first three parts of our project "Image Magick Banner Generator" (IMBG). You'll probably want to save these in their own folder, because this project will have nine scripts total by the time we're done. I also break it down into smaller scripts, because the individual parts are useful on their own outside this context.

*Whew!* God, I hope I'm getting this right!

a random banner sig

The whole Image Magick Banner Generator project:

part 1 - get a random line from a file, applying bevels and borders to an image.
part 2 - generate a solid or gradient background.
part 3 - generate a pattern or plasma noise background.
part 4 - generate the text itself, and use all of the previous scripts to make a banner.
part 5 - a final wrapper script, and some funny results.
Project Tarball with all scripts, GPL'ed

Useful Image Magick links:

Image Magick home page manual
Examples of ImageMagick Usage - a fantastic suite of tutorials!
Fred's Image Magick scripts

Follow me on Twitter for an update every time this blog gets a post.
Stumble it Reddit this share on Facebook

suddenly the moon