ImageGenerator/page_generator.sh
2019-11-22 23:03:19 +01:00

35 lines
1.2 KiB
Bash
Executable File

#!/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=$( [[ "$LANDSCAPE" == "1" ]] && echo $LARGE_SIDE || echo $SMALL_SIDE )
TOTAL_HEIGHT=$( [[ "$LANDSCAPE" == "1" ]] && 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=$( dirname ~+ )/a4
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} -compress zip -quality 100 "$IMG" -geometry ${REQ_WIDTH}x${REQ_HEIGHT} -duplicate ${DUP},0 ${PDF_NAME} )
[ 0 -eq $? ] && echo $PDF_NAME || panic "An error occured... $OUT"