Those of you who follow me on Twitter probably know that I'm something of a geek-of-all-trades; I love reading and learning. When my employer, IBM, entered the world of Open Badges for employee credentials, I jumped in with both feet. We're encouraged to display our badges in email signatures, online, et cetera...but what is one to do when one has earned 50+ badges in a wide range of areas/disciplines? Now, obviously I can't put that many badges in an email signature, and a static display can only do so much...so the thought occurred to me, "I should be able to turn these into an animated image!" Time for some shell scripting...
Let's consider the source material:
- Our badging provider provides badges as 352x352 PNG images, so I don't have to worry about resizing individual images; I can treat them all the same way and resize the end product as needed.
- I'm not worried about the PNG metadata in this particular case, so converting to another format isn't a big deal.
- I could go with an animated PNG (APNG), but (a) not all browsers display APNGs properly, and (b) animating PNGs can result in images significantly larger than comparable animated GIFs
At this point, I'm ready to start playing with ImageMagick, a wonderfully complete (and open source!) image manipulation package. ImageMagick handles 200+ image formats, and is available as both a command-line toolbox and libraries/interfaces for a wide range of programming languages.
The actual construction of the animated GIF with ImageMagick is straightforward - it's a one-liner:
$ convert *.png -frame 5 -dispose Background -set delay 150 allmybadges.gif
The options put a frame around each image, dispose of the previous image before displaying the next one, and set the delay (in hundredths of seconds) before adding them to the end product, allmybadges.gif.
Wait, though...oh, I don't like that. Since many badges have similar names (e.g. task-level-1.png and task-level-2.png, ibm-iot-whatever1.png, ibm-iot-whatever2.png, etc.), my use of a wildcard puts them in alphabetical order...which is kind of boring when 2-3 consecutive badges share color schemes or other design elements. So, what I really want to do is randomize the badges before animating them. Well, bash provides $RANDOM as an environment variable, yielding pseudorandom integers between 0 and 32767; that should be sufficient for me to play with filenames.
I wound up with this script:
# badgeanim - create animated GIF of all PNG badges
cd $WORKDIR # move into working directory
rm $WORKDIR/*.png # blow away any old PNG files
for file in $BADGEDIR/*.png # grab copies of current PNGs
cp $file ./$RANDOM`basename "$file"` # prepend $RANDOM to filename
convert *.png -frame 5 -dispose Background -set delay 150 allmybadges.gif # ImageMagick builds the GIF
rm $WORKDIR/*.png # blow away leftover PNG files
The results can be seen at the top of this article and in the "My OpenBadges" widget on the right margin of the page. (Clicking on the animated GIF to the right takes the viewer to a static display of all badges.) I could resize the end product for use elsewhere, like so:
$ convert allmybadges.gif -resize 200x200 allmybadges-200x200.gif
Check out ImageMagick - it's definitely worth your time if you do anything at all with images.