Image Magick Sampling

Image magick is an incredibly useful library if you need to process a lot of images. This sampling script will resize a folder full of images to a specified size in pixels.

It’s set to a max of 1.9 mega pixels by default.

#!/bin/bash

dir=$1
let max='1900000'

cd $dir

for file in `ls $dir`
do

  echo "Looking at $file"

  geometry=`identify $file | awk '{print $3}'`
  width=`echo $geometry | sed 's/[^0-9]/ /g' | awk '{print $1}'`
  height=`echo $geometry | sed 's/[^0-9]/ /g' | awk '{print $2}'`

  let pix=$width*$height

  if [ $pix -gt $max ]; then

  echo "Resizing $file"
  mogrify "$file" -sample @$max "$file"

  fi

done