#!/bin/sh # reviewimg - Review some images, optionally rotate or name them # # Requires: ImageMagick # xloadimage # # Usage: # # reviewimg [-n] [-r option] [path] # # Accepts a file or directory as an argument. If omitted, the current # working directory will be used. # # Options: # # -n Choose only images changed today. If no path has been specified, the # current working directory is examined for images. # -r Specify how rotated images should be handled: # 'none' - Don't want to rotate images # 'copy' - Create rotated image as copy (this is the default option) # e.g. original: filename.jpg rotated: filenamer.jpg # 'replace' - Overwrite original file with rotated image # # TODO / BUGS: # o Quality of rotated images is worse than orginals - ImageMagick uses # default sampling factor - not same as original image. # o Have an option to ask for dest filename # o Don't process files that have been overwritten # o Option to name images # o Option to dump EXIF info (otherwise it is lost with 'replace') # o usage synopsis with -h option # # Author: Graham Bleach # Version: 0.1 # errors E_BADUSAGE=1 E_CMDFAIL=2 E_MISSING_SW=3 E_NO_X=4 # --- functions --- rotate () { while read -p "Rotate [0/90/180/270] degrees:" -n 1 response < /dev/tty do case "$response" in 0) DEG='0'; echo; return;; 9) DEG='+90'; echo; break;; 1) DEG='+180'; echo; break;; 2) DEG='+270'; echo; break;; *) echo -e "\nUnrecognised response $response";; esac done if [ "$ROTATE" = 'replace' ] then DESTFILE="$FILE" else DESTFILE="${IMGDIR}/${IMGBASEFN}r.${IMGEXTN}" if [ -e "$DESTFILE" ] then while read -p "File $DESTFILE exists, overwrite [y/n]" -n 1 \ response < /dev/tty do case "$response" in y) echo; break;; n) echo; return;; *) echo -e "\nUnrecognised response: $response";; esac done fi fi convert -rotate "$DEG" "$FILE" "$DESTFILE" >/dev/null 2>&1 RVAL=$? if [ $RVAL -ne 0 ] then echo "Error rotating file $FILE" exit $E_CMDFAIL fi } # -- option defaults -- # looking for new files NEWFILES=0 # filetypes supported by ImageMagick that should be ignored IGNORETYPES='TXT PDF' ROTATE='copy' # use this to store list of files processed declare -a PROCFILES # -- option parsing & checking while getopts "nr:" OPTION do case $OPTION in n) NEWFILES=1;; r) ROTATE="$OPTARG";; ?) exit $E_BADUSAGE;; esac done shift $(($OPTIND - 1)) if [ -z "$1" ] then IMGPATH='.' else IMGPATH="$1" fi if [ -d "${IMGPATH}" ] then MAXDEPTH=1 else MAXDEPTH=0 fi if [ $NEWFILES == 1 ] then FINDOPTS='-ctime -1' else FINDOPTS= fi if [ "$ROTATE" != 'copy' ] && [ "$ROTATE" != 'replace' ] \ && [ "$ROTATE" != 'none' ] then echo "Invalid rotation option: $ROTATE" exit $E_BADUSAGE fi # Check ImageMagick installed which identify >/dev/null 2>&1 RVAL=$? if [ $RVAL != 0 ] then echo "ImageMagick appears not to be installed!" exit $E_MISSING_SW fi # Check xview / xloadimage installed which xview >/dev/null 2>&1 RVAL=$? if [ $RVAL != 0 ] then echo "xview appears not to be installed!" exit $E_MISSING_SW fi # find geometry of root window # if there's an error, maybe X is not in use ;-) XINFO=$(xwininfo -root) RVAL=$? if [ $RVAL != 0 ] then echo "ERROR: Cannot get attributes of X root window ..." echo "Perhaps X is not running?" exit $E_NO_X fi # get X geometry info XWIDTH=$(echo ${XINFO} | sed 's/^xwininfo: .*Width: \([0-9]\{1,\}\) .*$/\1/') XHEIGHT=$(echo ${XINFO} | sed 's/^xwininfo: .*Height: \([0-9]\{1,\}\) .*$/\1/') find "${IMGPATH}" -type f -maxdepth ${MAXDEPTH} ${FINDOPTS} -print | while read FILE do ID=$(identify -format '%m:%w:%h:%e:%t:%d' "${FILE}" 2>/dev/null) RVAL=$? if [ $RVAL != 0 ] then # echo "Cannot identify file ${FILE} - probably not an image. Ignored." continue fi # if someone uses ':' in filenames we're in trouble ;-) IMGFORMAT=$(echo ${ID} | cut -d':' -f1) IMGWIDTH=$(echo ${ID} | cut -d':' -f2) IMGHEIGHT=$(echo ${ID} | cut -d':' -f3) IMGEXTN=$(echo ${ID} | cut -d':' -f4) # file ext e.g. '.JPG' IMGBASEFN=$(echo ${ID} | cut -d':' -f5) # filename to ext e.g. 'IMG_1437' IMGDIR=$(echo ${ID} | cut -d':' -f6) # directory containing file # Some types should be ignored. For example, some formats are supported # by ImageMagick, but are not really images e.g. TXT, PDF for FMT in $IGNORETYPES do if [ "$IMGFORMAT" = "$FMT" ] then # ignore file - iterate outer loop continue 2 fi done # Viewing the picture with xview # we scale the picture to fit nicely on the screen if [ $IMGWIDTH -le $XWIDTH ] && [ $IMGHEIGHT -le $XHEIGHT ] then ZOOM='100%' else PERWID=$[${XWIDTH}*100/$IMGWIDTH] PERHT=$[${XHEIGHT}*100/$IMGHEIGHT] if [ $PERWID -gt $PERHT ] then ZOOM="$[${PERHT}-5]%" else ZOOM="$[${PERWID}-5]%" fi fi xview -zoom "$ZOOM" "$FILE" >/dev/null 2>&1 RVAL=$? if [ $RVAL -ne 0 ] then echo "Error viewing file $FILE" exit $E_CMDFAIL fi if [ "$ROTATE" != 'none' ] then rotate fi done