Information wants to be free...

Image Scaler Script

When uploading images taken with a digital camera to the interwebs, I always want to scale/resize it to a smaller size. To automate this process is of course a no-brainer, but the trick is to consider the aspect of the image. I like to use the size 800x600, but images taken with a 90 degree angle on the camera should be resized to 600x800 instead.

This bourne shell script uses ImageMagick to do the job:

#!/bin/sh

for i in "$@"; do
  INFO=`identify "$i"` || continue;
  WIDTH=` echo "$INFO" | sed -r -e 's/.* ([0-9]+)x[0-9]+ .*/\1/'`
  HEIGHT=`echo "$INFO" | sed -r -e 's/.* [0-9]+x([0-9]+) .*/\1/'`
  if [ $WIDTH -ge $HEIGHT ]; then
    echo "$i: W > H"
    mogrify -scale 800x600 "$i"
  else
    echo "$i: H > W"
    mogrify -scale 600x800 "$i"
  fi
done
          


Topic: Scripts and Code, by Kjetil @ 01/03-2010, Article Link