Jérôme Belleman
Home  •  Tools  •  Posts  •  Talks  •  Travels  •  Graphics  •  About Me

Rotating Images for the Web with GraphicsMagick

22 Jul 2017

GraphicsMagick is great for massively processing photos. Rotating them using Exif whether they're landscapes or portraits is a common application for the Web.

1 Rotating Pictures According to Exif Information

Photos you take with a camera will include tags describing extra information about the picture, such as when it was taken, sometimes where, how the camera was set up and, very usefully, in which orientation – landscape or portrait. However, web browsers may not take those tags into account and you will want to literally turn portraits 90°. Luckily, GraphicsMagick can use Exif data and offers the -auto-orient option to automatically rotate the image according to it. This is convenient because you don't have to first sort landscapes from portraits – you can treat all your photos the same way and GraphicsMagick will do the right thing:

cd in
mkdir ../out
for i in *.jpg; do gm convert -auto-orient $i ../out/$i; done

2 Removing Exif Data

What might feel a bit surprising, though, is that opening in an Exif-aware image viewer the portrait which you just subjected to -auto-orient will display it in the incorrect orientation. That's because the Exif data was left untouched by GraphicsMagick and the image viewer assumes that it still needs to be rotated. For the purpose of this post, which is to publish pictures on the web, let's just say it's best to altogether remove all Exif data. This is also something GraphicsMagick can do for you, even if the name of the option is a tad misleading and quite frankly hard to remember – +profile \*:

for i in *.jpg; do gm convert +profile \* $i ../out/$i; done

For GraphicsMagick, Exif is just one type of profile. And in its logic, -profile adds a profile, whereas +profile removes it. The * argument targets all profiles.

3 Resizing Pictures With the Right Edge

Say you want to keep some consistency across all the pictures of all your galleries and have a long edge of the same length. The problem is that the long edge may be the width or the height of the picture, depending on whether it's a landscape or portrait, respectively. Thankfully, the -geometry option is clever enough that, when provided a single value, it will set it for the longest edge, regardless of whether there is still Exif data or not. In other words, running:

for i in *.jpg; do gm convert -geometry 1280 $i ../out/$i; done

... will cause a 3264×2448 landscape to be scaled down to 1280×960 and a 2448×3264 portrait to be scaled down to 960×1280.

Assuming you may have pictures smaller than 1280 px wide or high you may even want to resize only if it means downscaling, not if it means enlarging. Simply adding > to the dimension does the business:

for i in *.jpg; do gm convert -geometry 1280\> $i ../out/$i; done

4 The Whole Command Line

Clearly you don't want to run GraphicsMagick multiple times to perform each little change, not least because it would recompress the image each time, losing quality with each pass. You can bring all the options together in a single command line to eventually recompress only once:

for i in *.jpg; do
    gm convert -auto-orient +profile \* -geometry 1280\> $i ../out/$i
done

5 References