#!/bin/bash # jpegmunge # by Jon Jensen # 2004-09-12 transform= rename= while [ "${1:0:1}" = "-" ] do if [ $1 = "-90" ]; then transform="$transform -rotate 90" elif [ $1 = "-180" ]; then transform="$transform -rotate 180" elif [ $1 = "-270" ]; then transform="$transform -rotate 270" elif [ $1 = "-o" ]; then transform="$transform -optimize" elif [ $1 = "-r" ]; then rename="$rename -nf%Y%m%d-%H%M%S" elif [ $1 = "-t" ]; then rename="$rename -ft" else echo "Ignoring unknown option $1" >&2 fi shift done if [ $# -lt 1 ]; then cat <&2 Usage: $0 [-r] [-t] [-o] [-90|-180|-270] file1 [file2 ...] File metadata options (calls jhead): -r rename to YYYYMMDD-HHMISS.jpg based on EXIF creation timestamp -t set timestamp of file to EXIF creation timestamp Image transform options (calls jpegtran): -o optimize (non-lossy) -90 rotate 90 degrees -180 rotate 180 degrees -270 rotate 270 degrees Transforms roll file's timestamp ahead by 1 second so that image viewers can see the file changed and refresh the display. Preserves ownership, creation and modification timestamps (with the exception of the 1 second increment), and all EXIF data. Transforms are saved to a temporary file first to avoid data loss in case of a crash or abort. EOF exit 1 fi tmpfile=.jpegrotate.tmp.$$ for i do if [ -n "$transform" ]; then jpegtran -trim -copy all $transform -outfile $tmpfile "$i" if [ $? -eq 0 ]; then if [ "$EUID" -eq 0 ]; then chown --reference="$i" $tmpfile else chgrp -f --reference="$i" $tmpfile fi chmod -f --reference="$i" $tmpfile #touch -c --reference="$i" -F 1 $tmpfile touch -c --reference="$i" $tmpfile mv -f $tmpfile "$i" echo "Transformed $i" else rm -f $tmpfile echo "Error transforming $i" >&2 fi fi [ -n "$rename" ] && jhead $rename "$i" done