convert

Do you want to make copies of your images with different resolutions quickly?

Imagemagics’s convert tool is pretty good at this. Following shell script can be easily adjusted to make as many different resolutions as you need. It only reduces images and keeps their aspect ratio… but you can easily adjust settings for each resolutions.

To speed up processing, you can adjust number of background jobs the script runs.

#!/bin/bash

# this script converts your source images to multiple sets of images with different resolutions / settings
# argument 1 must be directory, where your images reside
# it will then create subdirectories for each resolution
# NO SPACES IN FILENAMES!
# files are only resized down, smaller images are not enlarged

# processing settings
# number of background jobs to run at once - change it according to your CPU
bg=8

declare -a resolutions
declare -A options

# populate resolutions array with wanted resolutions
resolutions[0]="128x96"
resolutions[1]="1024x768"
resolutions[2]="1920x1080"

# populate options array with wanted options - make sure array keys match with specific resolution
options["128x96"]="-auto-orient -verbose -quality 80%"
options["1024x768"]="-auto-orient -verbose -quality 80%"
options["1920x1080"]="-auto-orient -verbose -quality 80%"

# output image type/extension
out="jpg"

# functions
usage()
{

        echo "Usage: $0 <dirname>"
        exit 1

}

# check is catalog provided as first argument
if [ $# -ne 1 ]; then
  echo "Provide directory containing images as first argument" 
  usage
fi
if [ ! -d ${1} ]; then
  echo "First argument is not directory"
  usage
fi

echo "started at `date`"
echo "Processing files in directory $1"
cd $1

# cycle trough all given resolutions 
for resolution in "${resolutions[@]}"; do

  mkdir -p ${resolution}

  for f in `ls -1 *.*`;do

    # get filename and extension
    ext=`echo $f |awk -F . '{print $NF}'`
    base=`basename $f .${ext}`

    echo "convert $f -resize ${resolution}\> ${options[${resolution}]} ./${resolution}/${base}.${out}"
    convert $f -resize ${resolution}\>  ${options[${resolution}]} ./${resolution}/${base}.${out} &

    #job control
    while [[ $( jobs | wc -l ) -ge $bg ]]; do
      sleep 1
    done
  
  done

done

echo "finished at `date`"

ImageMagic command-line tools and info can be found here