scripts/graphics/apply-exif-orientation.sh

46 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
#
# apply-exif-orientation.sh, Copyright © 2016 Matteo Cypriani
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#
# This script gets the Orientation EXIF tag from an image file (with
# exiftool) and rotates the image (with convert) to apply the
# orientation to the image data; the EXIF Orientation tag is deleted.
#
# About EXIF orientations, see:
# http://jpegclub.org/exif_orientation.html
if [ $# -ne 1 ] ; then
echo "Usage: $0 <image_file>" >&2
exit 1
fi
SOURCE="$1"
ORIENTATION=$(exiftool -n -veryShort -Orientation "$SOURCE" | sed -n 's/^.*: //p')
if [ "$ORIENTATION" = "" ] ; then
echo "No EXIF Orientation tag. Nothing to do."
exit 0
fi
case $ORIENTATION in
1) echo "Horizontal orientation. Nothing to do." ; exit 0 ;;
3) ORIENTATION=180 ;;
6) ORIENTATION=90 ;;
8) ORIENTATION=-90 ;;
*) echo "Orientation code $ORIENTATION unknown" >&2 ; exit 2 ;;
esac
echo "Rotating image by $ORIENTATION°"
TMP=$(mktemp)
convert -rotate $ORIENTATION "$SOURCE" "$TMP"
exiftool -quiet -overwrite_original_in_place -Orientation= "$TMP"
chmod a+r "$TMP"
mv "$TMP" "$SOURCE"