Meet my ClueBat. I call it 'HeadOn', because I apply it directly to the forehead.

Image Magick Banner Generator - part 2

Date/Time Permalink: 03/18/08 01:18:43 pm
Category: Graphics Tutorials

Today's pieces of the banner generator puzzle are a couple of scripts to generate backgrounds. We'll start with the most basic one.

I have this script saved as "":


if [ "$1" ]; then
  echo "USAGE SIZE (XxY)"
  exit 1

  BGCOLOR=$(./ /usr/X11R6/lib/X11/rgb.txt | cut -f 3- | sed 's/[ \t]*//g')
  convert -size $SIZE xc:"$BGCOLOR" background.png

exit 0

...almost self-explanatory. Given an XxY argument, it produces a random solid-color background. "./ 400x100" gets you something like this:

solid background example

Later on in the project, we'll see how we use Image Magick's 'identify' command to get the size of a banner made with text, so we know how to pass that size argument to the background scripts. Right now, let's deal with a thornier problem, generating a gradient and not having it just be straight up and down.

I have this script saved as "":


if [ "$1" ]; then
  echo "USAGE SIZE (XxY)"
  exit 1

  FROMCOLOR=$(./ /usr/X11R6/lib/X11/rgb.txt | cut -f 3- | sed 's/[ \t]*//g')
  TOCOLOR=$(./ /usr/X11R6/lib/X11/rgb.txt | cut -f 3- | sed 's/[ \t]*//g')
  STARTSIZE=$(($(echo $SIZE | awk -Fx '{print $1}')+10))

  CONVERTSTRING="$CONVERTSTRING"" -rotate ""$(($RANDOM % 360 + 1))"
  CONVERTSTRING="$CONVERTSTRING"" -gravity center -crop ""$SIZE""+0+0"


exit 0

...not nearly as straight-forward as we had hoped. See, first we pick two random colors, then we have to get the 'X' width (the longest dimension) and pad it some, then crop it back down to our expected size. Let's show why we have to do it this way:

If we run:

convert -size 400x100 gradient:red-blue background.png

we get this:

straight background

If we want a gradient that goes some other orientation besides straight up and down, we can simply add the '-rotate' option, right?

convert -size 200x50 gradient:red-blue -rotate 45 background.png

But I see a problem:

caddywumpus background

and cropping it down to the original size doesn't help:

convert -size 200x50 gradient:red-blue -rotate 45 -gravity center -crop 200x50+0+0 background.png

wrong background what we have to do is generate the gradient at a much larger size, say a square with its size as the widest dimension of the desired banner. How's that working for us?

convert -size 200x200 gradient:red-blue -rotate 75 -gravity center -crop 200x50+0+0 background.png

clipped background

We almost got away with that! But look closely and you'll see, for this rotation of 75 degrees, we ended up with a corner chipped out of the upper-right and lower-left.

Sooooo, we have to add an extra ten pixels on the border of the square, which we can then crop down to the proper size for a banner background. And that explains all the calisthenics in the script... hopefully.

Running it gives backgrounds like these:

correct gradient background

correct gradient background

correct gradient background

correct gradient background

How trendy! We'll explore patterned backgrounds next.

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