GraphicsMagick Recipes

Graphicsmagick is a free com­mand-line tool for edit­ing im­ages - It’s great for mak­ing sim­ple changes to a whole bunch of im­ages at once.

Having to scale a bunch of im­ages to three dif­fer­ent sizes while also chang­ing the file type is some­thing that’s been com­ing up in my work lately. This is easy enough to do in Lightroom, but it feels icky to fire up a mas­sive piece of soft­ware just for one sim­ple task. GraphicsMagick does things like ro­tate, scale and con­vert im­ages just as well, but with a much smaller foot­print.

I’ll keep us­ing Lightroom for more ad­vanced photo edit­ing, but for the sim­ple stuff Graphicsmagick is great.

(This is a com­mand-line thing)

If you’re al­ready com­fort­able with the com­mand-line skip right ahead. If not, stick around - it’s re­ally not that hard to use. Jim Hoskins has a very good in­tro­duc­tion to the Mac OS X Command Line on Treehouse. Once you read that you’ll be ready to fol­low along with the rest of the ar­ti­cle.

(If you’re on Windows like my­self, read the Treehouse ar­ti­cle any­way. The Windows com­mand-line is fun­da­men­tally the same thing as the Mac OS X com­mand-line - you’ll fig­ure it out pretty quickly.)

Let’s in­stall Graphicsmagick

On a Mac the eas­i­est way to in­stall Graphicsmagick is through Homebrew. Homebrew is a com­mand-line app that makes it eas­ier to in­stall other com­mand-line apps. Once you’ve got it set up, run the fol­low­ing com­mand to in­stall Graphicsmagick:

brew install graphicsmagick

On Windows grab the lat­est ver­sion from the pro­ject web­site and fol­low the in­struc­tions.

Once the setup is com­plete, open a new com­mand line and type gm (short for Graphicsmagick). If every­thing is set up cor­rectly you should see the fol­low­ing re­sult:

> gm
GraphicsMagick 1.3.24 2016-05-30 Q8 http://www.GraphicsMagick.org/
Copyright (C) 2002-2016 GraphicsMagick Group.

And you’re good to go! Here’s some ex­am­ples of things to do:

Resize a folder of im­ages

gm mogrify -output-directory your-output-folder -create-directories -resize 400x200 *.jpg

Let’s look at this one bit at a time.

Next, we’ll pass a num­ber of ar­gu­ments to mogrify that tell it ex­actly what to do.

This gm [command] [arguments] [source] struc­ture re­mains largely the same re­gard­less of which com­mand you’re us­ing. Here’s some more ex­am­ples:

Convert a folder of im­ages to a dif­fer­ent for­mat

gm mogrify -output-directory output -format png *.jpg

GM will con­vert pretty much any im­age file into any­thing you could think of - the list of sup­ported file types is im­pres­sive

Create an an­i­mated gif from a folder of im­ages

gm convert -delay 100 *.jpg animation.gif

-delay de­fines the de­lay be­tween each frame of the an­i­ma­tion in mil­lisec­onds.

Generate a grid from a folder of im­ages

gm montage -tile 5x5 -geometry 250x250+5+5 *.jpg grid.jpg

-tile spec­i­fies how many columns and rows the mon­tage should have. -geometry de­fines the di­men­sions of each in­di­vid­ual im­age in the mon­tage and the spac­ing around it - in this case 250px by 250x with 5px spac­ing on ei­ther side.

RGB to CMYK Separations

I’ve writ­ten a batch script based on this Stack Overflow an­swer:

REM Convert to CMYK
gm convert %1.jpg -colorspace CMYK %1-cmyk.jpg
REM Invert
gm convert %1-cmyk.jpg -operator All negate 1 %1-cmyk.jpg

REM Generate individual channels
gm convert %1-cmyk.jpg -channel Cyan %1-cyan.png
gm convert %1-cmyk.jpg -channel Yellow %1-yellow.png
gm convert %1-cmyk.jpg -channel Magenta %1-magenta.png
gm convert %1-cmyk.jpg -channel Black %1-key.png

Usage:

rgbToCMYK.bat myImage

Where myImage is the file­name with­out the file ex­ten­sion.

Write the file­name into the im­age

gm mogrify  -output-directory output -fill white -pointsize 25 -font Arial -draw "text 10,30 '%t'" *.png

The coolest thing: You can com­bine any of these com­mands

This is the great thing about com­mand-line tools like this: They don’t make any as­sump­tions about what you are go­ing to use them for. So you could com­bine any of these com­mands (and many more) in any or­der you liked with just a few key­strokes.

As an ex­am­ple, you might want to cre­ate an an­i­mated gif from a folder of im­ages but also scale the gif so you don’t end up with a mas­sive file. Just pass a -resize ar­gu­ment to convert and you’re set.

gm convert -resize 200x200 -delay 100 *.jpg animation.gif

These are some of the ways I use GM in my work - let me know if you have any more sug­ges­tions!