ImageOptim is a popular tool used to optimize images for web usage, reducing their file size without compromising visual quality. However, one common issue encountered with ImageOptim is the loss of rotation metadata, resulting in incorrectly displayed images. In this blog post, I will discuss the problem in detail and present a solution using a custom script that leverages mogrify and exiftool. This script helps to restore the natural rotation state of images, ensuring they retain their intended orientation even after optimization with ImageOptim.

When images undergo optimization in ImageOptim, the tool effectively removes unnecessary metadata, including rotation information. This metadata is essential for proper image display, particularly when images are taken with cameras that record orientation data.

Without the rotation metadata, images can appear rotated incorrectly when viewed in applications that rely on this information. For example, an image that was originally taken in portrait mode may appear rotated sideways or upside down after optimization with ImageOptim.

To overcome this limitation, I have developed a custom script that uses mogrify and exiftool to resave images in their natural rotation state, while simultaneously removing the rotation metadata. By implementing this script in your workflow, you can preserve the intended orientation of images even after optimization with ImageOptim.

To access the complete solution script, please refer to this Gist. The script utilizes mogrify, a command-line tool for batch image processing, and exiftool, a powerful utility for reading and modifying metadata. Note that just like ImageOptim this script modifies the original image and should therefore be called on a duplicate set of images, not the originals.

#!/bin/sh

if [ -z "$1" ]; then
  echo "Usage: $0 <directory>"
  exit 1
fi

dir="$1"

if [ ! -d "$dir" ]; then
  echo "Error: '$dir' is not a directory"
  exit 2
fi

for file in "$dir"/*.jpg; do
  if [ -f "$file" ]; then
    echo "Processing: $file"
    mogrify -auto-orient "$file"
    exiftool -Orientation= -n -overwrite_original "$file"
  fi
done

The script functions by leveraging mogrify to apply the necessary rotation adjustment based on the image’s metadata. It uses exiftool to extract the rotation information and determines the correct rotation angle. The image is then resaved without the rotation metadata, ensuring its natural orientation is preserved.

For a comprehensive understanding of the issue and related discussions, I recommend referring this ImageOptim GitHub issue. This discussion highlights the challenges faced by users regarding rotation loss during the optimization process.

By incorporating this custom script into your image optimization workflow, you can address the problem of rotation loss in ImageOptim. Preserving the natural rotation state of images ensures they appear as intended, regardless of the application or platform used to view them. Remember to use the provided script responsibly and explore further enhancements based on your specific requirements.