#!/bin/bash set -e SUBDIR="__jpeg_thumbs" TEMPFILE="$SUBDIR/$makethumbs.tempfile$" #THUMBSIZE="160x160" #THUMBSIZE="376x376" # Eighth square of the 3008x2000 - 376x376 (Nikon D70) #THUMBSIZE="752x752" # Quarter square of the 3008x2000 - 752x752 #THUMBSIZE="816x816" # Quarter square of the 3264x2448 - 816x816 (Canon S5) #THUMBSIZE="1296x1296" # Half the size in Canon S5 #THUMBSIZE="1600x1600" [ -z "$1" ] && { echo "$0: Give square side as a first argument. [Optionally give JPEG quality as the second arg.]" exit 1 } SIZE="$1" if [ "$SIZE" -lt 121 ]; then ADD="-normalize -sharpen 5" SUFFIX="_thumbnail" else ADD="" SUFFIX="" fi QUALITY="$2" [ -z "$QUALITY" ] && QUALITY=50 THUMBSIZE="${SIZE}x${SIZE}" [ -d "$SUBDIR" ] && { echo "$0: $SUBDIR already exists, exiting." exit 1 } mkdir "$SUBDIR" NUMFILES="`find -maxdepth 1 -type f -iname '*.jpg' -or -iname '*.jpeg' -or -iname '*.png' -or -iname '*.gif' | wc -l`" echo "Num files here: `find -maxdepth 1 -type f | wc -l`" echo "To process: $NUMFILES" echo "Thumbs size: $THUMBSIZE JPEG quality: $QUALITY" echo "Additional args: $ADD" echo "Shrinking and rotating where needed, stripping all embedded info." echo "PLEASE WAIT." echo NUMTRANS=0 find -maxdepth 1 -type f -iname '*.jpg' -or -iname '*.jpeg' -or -iname '*.png' -or -iname '*.gif' | sort | while read i; do NEWFILE="`echo "$SUBDIR/$i" | sed -e 's/IMG/img/' -e 's/\.jpe\?g/'$SUFFIX'.jpg/i' -e 's/\.png$/.jpg/i' -e 's/\.gif$/.jpg/i'`" nice convert $ADD -size "$THUMBSIZE" -quality "$QUALITY" -resize "$THUMBSIZE" +profile '*' "$i" "$NEWFILE" case `jpegexiforient -n "$i"` in 1) transform="";; 2) transform="-flip horizontal";; 3) transform="-rotate 180";; 4) transform="-flip vertical";; 5) transform="-transpose";; 6) transform="-rotate 90";; 7) transform="-transverse";; 8) transform="-rotate 270";; *) transform="";; esac if [ -n "$transform" ]; then nice jpegtran -copy none -optimize $transform "$NEWFILE" > "$TEMPFILE" if [ $? -ne 0 ]; then echo echo "### Error while transforming $NEWFILE - skipped." >&2 else rm "$NEWFILE" mv "$TEMPFILE" "$NEWFILE" ((NUMTRANS++)) fi fi touch -r "$i" "$NEWFILE" nice exiftool -qq -TagsFromFile "$i" '-DateTimeOriginal>FileModifyDate' "$NEWFILE" ((NUMFILES--)) SUBSIZE="`du -h $SUBDIR | cut -f1`" echo -en "\r$NUMFILES to be done, $NUMTRANS rotations done, $SUBSIZE so far, please wait... " done echo