#!/bin/bash
# resizePictures.sh
# By neo73 [at] rooot [dot] net - Last modified march 2010
# Resize big pictures in a folder.
# This script requires convert and identify (from ImageMagick) and exiftran.
# USE AT YOUR OWN RISK

PICT_EXT=jpg
MAX_PICT_WIDTH=2592
PICT_QUALITY=85

function usage {
  echo "Usage: $0 <folder>"
  exit
}

if [ ! $# -eq 1 ]; then
    usage
fi

dir=$1
if [ ! -d "$dir" ]; then
  echo "Error: \"$dir\" is not a folder"
  exit 0
fi

cd "$dir"
IFS_BAK=$IFS
export IFS='
'
for file in `ls *.$PICT_EXT`; do
    args=""

    echo -n '.'

    # First fix orientation
    orientation=$(identify -format "%[EXIF:Orientation]" "$file")
    if [ "$orientation" != "1" ]; then
        # convert won't update exif info so we use exiftran.
        exiftran -a -i "$file"
    fi

    #Then resize image
    convert -resize "$MAX_PICT_WIDTH>x$MAX_PICT_WIDTH>" -quality "$PICT_QUALITY" "$file" "$file"
done
echo

export IFS=$IFS_BAK
