diff --git a/photos2pdf b/photos2pdf index c51b184..6a63e11 100755 --- a/photos2pdf +++ b/photos2pdf @@ -28,8 +28,7 @@ sigmoidal_contrast="45,50%" # Number of colors result images are quantized to posterize=6 -# Keep intermediate images after processing (note that they will be removed in -# any case at the next run of this script) +# Keep intermediate images after processing keep_tmpfiles=true # Paper size of all pages @@ -42,10 +41,13 @@ offset="1cm 3cm" scale=0.75 # PDF backdrop template directory -templatedir=/var/lib/photos2pdf/templates +templatedir=/var/lib/photos2pdf/template # Filename of complete result PDF -result="pdf/result.pdf" +result_filename="result.pdf" + +# Directory of original files to process (initially unset) +dir= ## # Functions @@ -71,26 +73,127 @@ info() { log INFO "$1" } +print_help() { + cat << 'EOF' +photos2pdf +========== + +Description +----------- +Trim and brighten JPGs, produce PDF containing one photo per page. + +Usage +----- +```shell +photos2pdf [OPTION ...] DIR +``` + +Arguments +--------- +* `DIR`: Directory containing the original photos. + +Options +------- +* `--simplify INT`: Before morphology analysis, reduce + image dimensions by this factor + (default: 2). +* `--morphology STRING`: Morphology to apply for trimming + (default "Open:6"). +* `--morphology-shape STRING`: Shape of morphology to apply for + trimming (default: "Disk"). +* `--colors INT`: Number of colors images are quantized + to for component detection (default: 4). +* `--connected-components INT`: Neighbors to evaluate when detecting + connected components (default: 8). +* `--area_treshold INT`: Area treshold for connected components + detection (default: 1000). +* `--sigmoidal-contrast-trim STRING`: Sigmoidal contrast adjustment + used when trimming (default: "30,30%"). +* `--sigmoidal-contrast STRING`: Sigmoidal contrast adjustment visible + in result images (default: "45,50%"). +* `--posterize INT`: Number of colors result images are + quantized to (default: 6). +* `--keep-tmpfiles true|false`: Keep temporary files (default: false). +* `--paper STRING`: PDF page size (default: "a4paper"). +* `--offset STRING`: Offset in page where image should be + placed (default: "1cm 3cm"). +* `--scale FLOAT`: Factor to scale image by before placing + in page (default: 0.75). +* `--templatedir STRING`: PDF backdrop directory (default: + "/var/lib/photos2pdf/template"). +* `--result-filename STRING`: Filename of complete result PDF + (default: "result.pdf"). +EOF +} + ## # Arguments -dir=$1 +i=1 +options_done=false -if [[ -z $dir ]] ; then - error "Usage: $0 DIR" -fi +while [[ $i -le $# ]] ; do + arg=$(eval "echo \$$i") -origdir=$(readlink -f "$dir") + if ! "$options_done" ; then + case "$arg" in + -h|--help) + print_help + exit 0 + ;; + --simplify| \ + --morphology| \ + --morphology-shape| \ + --colors| \ + --connected-components| \ + --area-treshold| \ + --sigmoidal-contrast-trim| \ + --sigmoidal-contrast| \ + --posterize| \ + --keep-tmpfiles| \ + --paper| \ + --offset| \ + --scale| \ + --templatedir| \ + --result-filename) + var=$(echo "$arg" | sed -e 's|^\-\-||;s|\-|_|g;') + i=$((i+1)) + arg=$(eval "echo \$$i") + eval "$var=\"$arg\"" + i=$((i+1)) + continue + ;; + --) + options_done=true + i=$((i+1)) + continue + ;; + -*) + error "Unknown option \"$arg\"; aborted." + ;; + esac + fi -if [[ ! -d $origdir ]] ; then - error "Directory not found: $dir ($origdir)" -fi + case "$arg" in + *) + [[ -n $dir ]] && error "Only one input directory can be specified; aborted." + dir=$arg + i=$((i+1)) + ;; + esac +done + +[[ -z $dir ]] && error "Usage: $0 [OPTION ..] DIR" ## # Main Program job_id=$(printf "%s" "$origdir" | sha256sum | cut -d" " -f1) +origdir=$(readlink -f "$dir") + +[[ -d $origdir ]] || error "Directory $dir not found; aborted." + if [[ -n $TMP ]] ; then tmp_base=$TMP/photos2pdf/$job_id elif [[ -n $XDG_RUNTIME_DIR ]] ; then @@ -99,10 +202,6 @@ else tmp_base=/tmp/photos2pdf/$job_id fi -if ! mkdir -p "$tmp_base" ; then - error "Could not create temporary workign directory $tmp_base; aborted" -fi - indir="$tmp_base/in" tmpdir="$tmp_base/tmp" outdir="$tmp_base/out" @@ -189,9 +288,6 @@ for jpg in "$indir/"*.jpg ; do while(<>){ chomp; next unless /(\d+)x(\d+)\+(\d+)\+(\d+)/; - printf - STDERR - "DEBUG: component: $_\n"; printf "rectangle %d,%d,%d,%d\n", $1, $2, $1+$3, $2+$4; @@ -202,12 +298,6 @@ for jpg in "$indir/"*.jpg ; do uniq ) - printf "DEBUG: connected components in \"%s\":" "$tmp1" - for d in "${draw[@]}" ; do - printf " \"%s\"" "$d" - done - printf "\n" - if ! convert \ "$tmp1" \ -fill black -colorize 100 \ @@ -250,13 +340,6 @@ for jpg in "$indir/"*.jpg ; do } ' < "$inf") - if ! "$keep_tmpfiles" ; then - if ! rm -f -- "$tmp1" "$tmp2" "$trimmed" "$inf" ; then - warning "Could not remove temporary files in \"$dir/tmp\"." - rv=1 - fi - fi - info "Processing $base: crop=$crop ..." if ! convert \ @@ -307,7 +390,7 @@ for png in "out/"*.png ; do continue elif ! pdfjam \ --quiet \ - --outfile tmp \ + --outfile "$tmpdir" \ --paper "$paper" \ "$jam_landscape" \ --scale "$scale" \ @@ -334,15 +417,17 @@ while read -r pdf ; do input+=("$pdf") done < <(ls -- "$tmpdir/"*.backdropped.pdf) -if ! pdftk "${input[@]}" cat output "$result" ; then - warning "Could not concatenate \"$result\"." - rv=1 -elif ! rm -f -- "tmp/"*.pdf ; then - warning "Could not remove tempfiles in \"$dir/tmp\"." +if ! pdftk "${input[@]}" cat output "$pdfdir/$result_filename" ; then + warning "Could not concatenate \"$result_filename\"." rv=1 +elif ! "$keep_tmpfiles" ; then + if ! rm -f -- "$tmp_base" ; then + warning "Could not remove temporary files." + rv=1 + fi fi if [[ $rv -ne 0 ]] ; then - error "One or more errors during PDF generation; aborted." "$rv" + error "One or more errors; aborted." "$rv" fi