#!/bin/bash IMG=$1 COUNT_HORIZONTAL=$2 COUNT_VERTICAL=$3 LANDSCAPE=${4} panic(){ echo $@; exit 2; } [[ ! -f $IMG ]] && panic "$IMG does not exist" SMALL_SIDE=4000 LARGE_SIDE=5800 # /tmp/out.pdf PDF 567x822 567x822+0+0 16-bit sRGB 980KB 0.010u 0:00.009 read name type GEOMETRY canvas depth format size other <<< $( identify $IMG ) IMG_WIDTH=$( cut -d "x" -f 1 <<< $GEOMETRY ) IMG_HEIGHT=$( cut -d "x" -f 2 <<< $GEOMETRY ) TOTAL_WIDTH=$( [[ -n "$LANDSCAPE" ]] && echo $LARGE_SIDE || echo $SMALL_SIDE ) TOTAL_HEIGHT=$( [[ -n "$LANDSCAPE" ]] && echo $SMALL_SIDE || echo $LARGE_SIDE ) REQ_WIDTH=$(( $TOTAL_WIDTH / $COUNT_HORIZONTAL )) REQ_HEIGHT=$(( $TOTAL_HEIGHT / $COUNT_VERTICAL )) DUP=$(( ( $COUNT_HORIZONTAL * $COUNT_VERTICAL ) - 1 )) DATE=$( date +%s ) OUTPUT_DIR=/tmp FILENAME=$( basename $IMG ) PDF_NAME=${OUTPUT_DIR}/${DATE}_${FILENAME}.pdf OUT=$( montage -tile ${COUNT_HORIZONTAL}x${COUNT_VERTICAL} -density 200x200 -units pixelspercentimeter -border 0 -page ${TOTAL_WIDTH}x${TOTAL_HEIGHT} "$IMG" -geometry ${REQ_WIDTH}x${REQ_HEIGHT} -duplicate ${DUP},0 ${PDF_NAME} ) [ 0 -eq $? ] && echo $PDF_NAME || panic "An error occured... $OUT"