#!/bin/sh # # $Id$ # # Rotate one or more digital camera pictures. # # Usage: # USAGE='Usage: camrotate [-v] [-auto|-cw|-ccw|-flip] jpeg-files' # # Each given file is rotated by 90 degrees clockwise (default) or # counterclockwise using jpegtran. All digital-camera comments are # preserved. The thumbnail, if present, is not rotated. This may # cause some viewing software to display the thumbnail incorrectly. # # Options: # # -v Verbose mode: report each file to stderr as it is processed. # -auto Automatically determine rotation amount from # orientation information in ExIF tags (default if # orientation data present). # -cw Rotate 90 degrees clockwise (default if no # orientation data present). # -ccw Rotate 90 degrees counterclockwise. # -flip Rotate 180 degrees defAmount= verbose=false # # Argument processing # while [ $# -gt 0 ] do case "$1" in -auto) defAmount=auto ;; -cw) defAmount=90 ;; -ccw) defAmount=270 ;; -flip) defAmount=180 ;; -v) verbose=true ;; --) shift break ;; -*) echo "$USAGE" 1>&2 exit 2 ;; *) break ;; esac shift done case "$#" in 0) echo "$USAGE" 1>&2 exit 2 ;; esac # # Process all file arguments # for file do dir=`dirname $file` case "$file" in *.jpg) base=`basename "$file" .jpg` origExtension=jpg extension=jpg ;; *.jpeg) base=`basename "$file" .jpeg` origExtension=jpeg extension=jpg ;; *.JPG) base=`basename "$file" .JPG` origExtension=JPG extension=jpg ;; *.JPEG) base=`basename "$file" .JPEG` origExtension=JPEG extension=jpg ;; *) echo "Can't handle file '$file', skipping" 1>&2 continue ;; esac TMP="$dir/cre$$" trap "rm -f $TMP; exit 1" 1 2 15 trap "rm -f $TMP; exit 0" 13 case "$defAmount" in ''|auto) orientation=`metacam -a "$file" | grep -i orientation` orientation=`expr "$orientation" : '.*\([0-9][0-9]*\).*'` case "$orientation" in '') if [ "X$defAmount" = "Xauto" ] then echo "No orientation information in $file, skipped" \ 1>&2 amount=0 else amount=90 fi ;; 1) amount=0 ;; 6) amount=90 ;; 8) amount=270 ;; *) echo "Unrecognized orientation information in $file," \ "skipped" \ 1>&2 amount=0 ;; esac ;; *) amount=$defAmount ;; esac if [ $amount -ne 0 ] then $verbose && echo "Rotating $file $amount degrees clockwise" 1>&2 jpegtran -rotate $amount -copy all "$file" > "$TMP" trap "" 1 2 13 15 rm -f "$file" mv "$TMP" "$file" trap 1 2 15 trap 13 fi done