July 14, 2017

Automatically Rotate Pictures to the Right Orientation with ImageMagick

Some photo cameras, for instance the iPhone, produce photos where the image's orientation is specified in the EXIF meta data. When displaying the photo, software first reads this information and rotates the photo accordingly. In some situations the software rotation does not work. One example is when you embed pictures into web pages:

The fun part about this is that when you open the picture alone, the browser does its job correctly and displays the image correctly: click here. Silly situation. So you somehow have to produce a picture that doesn't require the browser to evaluate the EXIF meta data and rotate the image. You can do that as part of the image resizing process with ImageMagick.

I like to downsize my images to 1000 pixels using the following command:

magick mogrify -resize 1000 *

When searching for a solution to my problem, I stumbled upon a new flag: -auto-orient. This rotates the picture as specified in the EXIF meta data and deletes the meta data. The resulting picture is already rotated correctly and hence is displayed correctly when embedded into web pages:

So when you are downsizing pictures with ImageMagick just add that flag and you are done.

magick mogrify -auto-orient -resize 1000 *

No more incorrectly displayed pictures!

© holger 2015 - 2020 |