#!/bin/bash # crop parameters (left-right x top-bottom) in [px] CROP_PARAMS=142x5 # size reduction RESIZE_PARAMS=85% FULL_RES_OUPUT="screenshot-merged-raw.pdf" REDUCED_OUPUT="screenshot-merged.pdf" help () { echo ""; echo "Convert a series of *.png screenshots in the DIR into a merged .pdf." echo "via a combination of Iagemagick and img2pdf tools." echo "[Ver. 1.0, 2020-06-20 by ILC]"; echo "----------------------------------------------" echo "Usage (short): screenshot2pdf DIR/" echo "Usage (full): screenshot2pdf CROP RESIZE DIR/" echo "Example: screenshot2pdf 142x5 85% ./" echo "----------------------------------------------" echo "This will produce a 'screenshot-merged.pdf' in the DIR/." echo "Default CROP (left/right x top/bottom) in [px] = 142x5" echo "Default RESIZE = 85%" echo "See Imagemagick 'convert' manual for more details."; echo "" } if [[ $# -eq 0 ]]; then help; exit; fi # read working dir, removing a possible trailing "/", and other params if [[ $# -eq 3 ]]; then CROP_PARAMS=$1; RESIZE_PARAMS=$2; WORKING_DIR="${3%/}"; elif [[ $# -eq 1 ]]; then WORKING_DIR="${1%/}"; else echo "Incorrect number of arguments!"; help; exit 2 fi TEMP_DIR="$WORKING_DIR/temp_images_$RANDOM" # create a temp directory for conversion mkdir $WORKING_DIR/$TEMP_DIR mkdir $WORKING_DIR/$TEMP_DIR/reduced # trim and downsample all PNGs in the current directory i=1; echo "Processing '$(realpath $WORKING_DIR)/*.png'"; echo "with 'convert -shave $CROP_PARAMS -resize $RESIZE_PARAMS'" for filename in $WORKING_DIR/*.png; do new_filename=$(printf "%02d" $i) convert -shave $CROP_PARAMS "$filename" $TEMP_DIR/${new_filename}.png; convert -shave $CROP_PARAMS -resize $RESIZE_PARAMS +repage "$filename" $TEMP_DIR/reduced/${new_filename}.png; i=$((i+1)); printf "$new_filename " done # create a batched pdf printf "Done. \nConverting into pdf... " img2pdf --output $WORKING_DIR/$FULL_RES_OUPUT $TEMP_DIR/*.png img2pdf --output $WORKING_DIR/$REDUCED_OUPUT $TEMP_DIR/reduced/*.png printf "Done.\n" rm -r $TEMP_DIR