Auto-rotate images based on their Exif data with ImageMagick (BASH)

Most modern cameras will store orientation details in the EXIF data of images taken on them. So if you turn the camera portrait to take an image, software can tell that this image should be displayed portrait.

However, not all software checks the EXIF data first, so you can wind up with portrait images being displayed lying on their side in some software - this is an easy trap to fall into if your file manager does honour the EXIF data.

ImageMagick's convert command can be used to read in the EXIF data, check the orientation and then rotate the image if required.

Details

  • Language: BASH

Snippet

convert $IMAGE -auto-orient $IMAGE_OUTPUT

Usage Example

# Auto-rotate foo.jpg in place
convert foo.jpg -auto-orient foo.jpg

# Auto-Rotate all images within a directory in place
for i in `ls *JPG`; do convert "$i" -auto-orient "$i"; done

# Auto-Rotate all images but save into a subdir
mkdir op
for i in `ls *JPG`; do convert "$i" -auto-orient "op/$i"; done