Wanna be 1337? Read Penguin Pete!

Image Magick Banner Generator - part 3

Date/Time Permalink: 03/20/08 10:39:56 am
Category: Graphics Tutorials

In part 2, we learned how to generate solid backgrounds and gradients. In this part, I'll show you how to generate patterned and random noise backgrounds. First, the patterns, which are more basic:

Save it as "generate_background_pattern.sh"

#!/bin/bash

if [ "$1" ]; then
  SIZE="$1"
else
  echo "USAGE generate_background_pattern.sh SIZE (XxY)"
  exit 1
fi

  PATTERNS=( bricks checkerboard circles crosshatch crosshatch30 crosshatch45 fishscales \
            gray5 gray20 gray55 gray95 hexagons horizontal horizontalsaw hs_bdiagonal \
            hs_cross hs_diagcross hs_fdiagonal hs_horizontal hs_vertical left30 \
            left45 leftshingle octagons right30 right45 rightshingle smallfishscales \
            vertical verticalbricks verticalleftshingle verticalrightshingle verticalsaw )
  PATTERN=${PATTERNS[$(($RANDOM % 32))]}

  CONVERTSTRING=" -size ""$SIZE"
  CONVERTSTRING="$CONVERTSTRING"" pattern:""$PATTERN -normalize"
  CONVERTSTRING="$CONVERTSTRING"" -shade "$(($RANDOM % 150 +50))"x"$(($RANDOM % 60 +10))
  CONVERTSTRING="$CONVERTSTRING"" background.png"

  convert $CONVERTSTRING

exit 0

All of the patterns in the $PATTERNS array come built-in with Image Magick. So our task is simply to list them, and pick a random one. A step I elected not to make optional is to use the 'shade' argument as well, which is similar to bumpmapping and make the pattern stand out better.

Some sample patterns produced with a
"./generate_background_pattern.sh 400x100":

pattern background example

pattern background example

pattern background example

Pretty elementary...

The random noise one, on the other hand, is much busier:

"generate_background_noise.sh"

#!/bin/bash

if [ "$1" ]; then
  SIZE="$1"
else
  echo "USAGE generate_background_gradient.sh SIZE (XxY)"
  exit 1
fi

  CONVERTSTRING=" -size ""$SIZE "
  case $(($RANDOM % 6 +1)) in
    1) CONVERTSTRING="$CONVERTSTRING""plasma: -fill grey -colorize 75% " ;;
    2)
      FROMCOLOR=$(./random_line.sh /usr/X11R6/lib/X11/rgb.txt | cut -f 3- | sed 's/[ \t]*//g')
      TOCOLOR=$(./random_line.sh /usr/X11R6/lib/X11/rgb.txt | cut -f 3- | sed 's/[ \t]*//g')
      CONVERTSTRING="$CONVERTSTRING""plasma:""$FROMCOLOR""-""$TOCOLOR ""-fill gray -colorize 75% "
      ;;
    3) CONVERTSTRING="$CONVERTSTRING""plasma:""grey-grey -blur 0x5 " ;;
    4) CONVERTSTRING="$CONVERTSTRING""plasma:""white-white " ;;
    5) CONVERTSTRING="$CONVERTSTRING""plasma:""black-black ""-fill black -colorize 75% " ;;
    6) CONVERTSTRING="$CONVERTSTRING""xc: -fx rand() -blur 0x2 " ;;
  esac
  if [ "$(($RANDOM % 6))" = 1 ]; then
    CONVERTSTRING="$CONVERTSTRING""-blur 0x2 -emboss .2 "
  fi
  if [ "$(($RANDOM % 6))" = 1 ]; then
    CONVERTSTRING="$CONVERTSTRING""-charcoal 3 -negate "
  fi
  if [ "$(($RANDOM % 6))" = 1 ]; then
    CONVERTSTRING="$CONVERTSTRING""-edge 1 "
  fi
  case $(($RANDOM % 5 +1)) in
    1) CONVERTSTRING="$CONVERTSTRING""-fx intensity " ;;
    2) CONVERTSTRING="$CONVERTSTRING""-blur 0x""$(($RANDOM % 3 + 1)) " ;;
    3) CONVERTSTRING="$CONVERTSTRING""-colorspace gray " ;;
    4) CONVERTSTRING="$CONVERTSTRING""-shade ""$(($RANDOM % 180 + 0))""x""$(($RANDOM % 10 + 5)) " ;;
    5) CONVERTSTRING="$CONVERTSTRING " ;;
  esac

  CONVERTSTRING="$CONVERTSTRING""background.png"
  convert $CONVERTSTRING

exit 0

Oooga! See, the noise generators that come built-in with Image Magick often produce patterns that are far too strong to read any text in front of them. So we have to soften them with filters like 'blur', 'edge', and 'charcoal'. But some of them are better without. Finally, the built-in plasma generator produces radically different results depending on what color arguments you give it:

white-white:
white plasma

black-black:
black plasma

...or even giving it no arguments at all. So it's better to treat these various "colors" of plasma as their own separate noise generators, to say nothing of the "xc: -fx rand()" method, which gives a similar pixel-static every time:

static noise

By the way, the '-fx' argument cloaks a vast new unexplored territory, being a built-in mini-language of math formulas applied to RGB color space. I'm still figuring that one out.

In a nutshell, you want some variety, but you want to keep the chaos controlled just enough so it doesn't spoil too many banners.

Anyway, here's a few examples produced by this module with
"./generate_background_noise.sh 400x100":

noise background example

noise background example

noise background example

noise background example

As you can see, you can get anything from a stucco texture to a marble look, and much more.

That takes care of all the backgrounds. The only thing left is to generate some text, slap it on a background, and Bonnie's your aunt! We'll get to the big beast of a script that does that next time.

yet another random 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