I wanted to use ImageMagick to automatically create thumbnails with captions. It usually comes installed on Linus and Mac OS X. To create an image out of a string, you run the following right from your terminal.

convert -background none -fill gray -gravity center \
  -size 140x45 caption:'STUFF' gray.png

You can then overlay this newly created image over a thumbnail you had:

composite -blend 80 -geometry +20+90 gray.png \
  thumbnail.png composed.png

The default font is nothing to write home about. When I tried running convert with a font parameter, however, it would say something like

convert: unable to read font `Gecko' @ warning/annotate.c/RenderType/862.

Turns out Gecko was not available on my system. But what is?

There’s a nice and short command to check that.

convert -list font

Output is somewhat verbose…

Path: /etc/ImageMagick/type-ghostscript.xml
Font: AvantGarde-Book
family: AvantGarde
style: Normal
stretch: Normal
weight: 400
glyphs: /usr/share/fonts/default/Type1/a010013l.pfb
Font: AvantGarde-BookOblique
family: AvantGarde
style: Oblique
stretch: Normal
weight: 400
glyphs: /usr/share/fonts/default/Type1/a010033l.pfb
...

I just need the names. Using grep to filter and sed to remove extra information, we get something much better.

convert -list font | grep "Font:" | sed "s/Font: //g"

Output:

AvantGarde-Book
AvantGarde-BookOblique
...

Now, it’s good to know the names of the fonts available but how do they look like? I can use ImageMagick to generate pictures of some strings using those fonts!

Here’s a bash script that does just that:

#!/bin/bash
FONTS=`convert -list FONT | grep "Font:" | sed "s/Font: //g"`
X=0
Y=5
SIZEX=300
SIZEY=30
WIDTH=900
HEIGHT=900
convert -size "${WIDTH}x${HEIGHT}" canvas:lightgray composed.png
for FONT in $FONTS;
do
  convert -background none -fill gray -size ${SIZEX}x${SIZEY} \
    -gravity center -font $FONT caption:"$FONT" gray.png
  convert -background none -fill white -size ${SIZEX}x${SIZEY} \
    -gravity center -font $FONT caption:"$FONT" white.png
  composite -geometry +$X+$Y gray.png composed.png composed.png
  let X=X+1
  let Y=Y-1
  composite -geometry +$X+$Y white.png composed.png composed.png
  let X=X-1
  let Y=Y+SIZEY+2
  if [ "$Y" -gt "$HEIGHT" ]; then
    let Y=5
    let X=X+SIZEX
  fi
  echo $FONT
done

Here’s the result:

enter image description here

Apart from convert and composite described above, there’s a bit of sizing and position calculations just because I wanted to print them all onto one big canvas. I also print them in two colors with an offset to create shadow effect. Otherwise, you could just make a separate file for each and print black on white and it would look like this –

 #!/bin/bash
FONTS=`convert -list FONT | grep "Font:" | sed "s/Font: //g"`
SIZEX=300
SIZEY=30
convert -size "${SIZEX}x${SIZEY}" canvas:white canvas.png
for FONT in $FONTS;
do
  convert -background none -size ${SIZEX}x${SIZEY} \
    -gravity center -font $FONT caption:"$FONT" font.png
  composite font.png canvas.png "$FONT".png
done

When done, you’d have images that look like this:

enter image description here

Share →