HOWTO : batch composite picture of large collection

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
contremaitre
Posts: 6
Joined: 2013-04-14T12:00:39-07:00
Authentication code: 6789

HOWTO : batch composite picture of large collection

Post by contremaitre »

Hi,

I'd like to share my script.
It takes all pictures in a folder and creates multiple composite pictures.
The number of pictures in each composite output is a parameter of the script.

The issues this script address are :
- Using montage directly on a large picture set results in high memory usage, as it reads all pictures at once before rendering
- Resize input picture before processing.
- Automaticaly turns the pictures to have a good ratio

Usage : set the parameters at the top of the script, and run it in the folder you have your pictures.

Here is the script :

Code: Select all

#!/bin/bash
#Parameters
ROW=2
COLUMN=1
EXTENSION=*.JPG
OUTPUT_DIR=montage21
OUTPUT_NAME=montage.jpg
#max width (or height) of input picture, 0 to disable
MAX_INPUT_WIDTH=1000
#end parameters

let picNumber=${COLUMN}*${ROW}
echo $picNumber
TILE=${COLUMN}x${ROW}
if [ "$ROW" -lt "$COLUMN" ]; then
  ROTATE_SIGN="<"
else
  ROTATE_SIGN=">"
fi
RESIZE=""
if [ "$MAX_INPUT_WIDTH" -gt 0 ]; then
  RESIZE="-resize $MAX_INPUT_WIDTH"x"$MAX_INPUT_WIDTH"
fi
ROTATE="-90$ROTATE_SIGN"
mkdir -p $OUTPUT_DIR
files=($EXTENSION)
for (( i=0; i<${#files[@]} ; i+=$picNumber )) ; do
    echo "processing ${files[@]:$i:$picNumber}"
    convert ${files[@]:$i:$picNumber} $RESIZE -rotate $ROTATE miff:-|montage - -tile ${ROW}x${COLUMN}  -geometry +0+0 $OUTPUT_DIR/${files[$i]}
done
Post Reply