From 822b9846782e86be5405a0f10cc4d06dbfd3b9bf Mon Sep 17 00:00:00 2001 From: Tilman Kranz Date: Mon, 15 Jul 2024 08:10:04 +0200 Subject: [PATCH] add option to not post-process pictures --- photos2pdf | 235 +++++++++++++++++++++++++++++------------------------ 1 file changed, 129 insertions(+), 106 deletions(-) diff --git a/photos2pdf b/photos2pdf index db490f9..ed004da 100755 --- a/photos2pdf +++ b/photos2pdf @@ -48,7 +48,7 @@ sigmoidal_contrast="45,50%" posterize=6 # Keep intermediate images after processing -keep_tmpfiles=true +keep_tmpfiles=false # Paper size of all pages paper="a4paper" @@ -111,8 +111,14 @@ Arguments --------- * `DIR`: Directory containing the original photos. -Options -------- +General Options +--------------- +* `--help`: Print this message and exit. +* `--direct`: Do not perform post-processing; use + original image. + +Post-Processing Options +----------------------- * `--simplify INT`: Before morphology analysis, reduce image dimensions by this factor (default: 2). @@ -148,6 +154,7 @@ EOF ## # Arguments +direct=false i=1 options_done=false @@ -160,6 +167,11 @@ while [[ $i -le $# ]] ; do print_help exit 0 ;; + --direct) + direct=true + i=$((i+1)) + continue + ;; --simplify| \ --morphology| \ --morphology-shape| \ @@ -262,117 +274,126 @@ if [[ $rv -ne 0 ]] ; then error "One or more errors during input file naming; aborted." "$rv" fi -for jpg in "$indir/"*.jpg ; do - base=$(basename "$jpg" .jpg) - tmp1="$tmpdir/$base.tmp1.png" - tmp2="$tmpdir/$base.tmp2.png" - com="$tmpdir/$base.com.png" - trimmed="$tmpdir/$base.trimmed.png" - inf="$tmpdir/$base.txt" - out="$outdir/$base.png" +if "$direct" ; then + for jpg in "$indir/"*.jpg ; do + base=$(basename "$jpg" .jpg) + out="$outdir/$base.png" + info "Resizing $jpg to $out ..." + convert -resize 1024 -dither Riemersma -colors 24 "$jpg" "$out" + done +else + for jpg in "$indir/"*.jpg ; do + base=$(basename "$jpg" .jpg) + tmp1="$tmpdir/$base.tmp1.png" + tmp2="$tmpdir/$base.tmp2.png" + com="$tmpdir/$base.com.png" + trimmed="$tmpdir/$base.trimmed.png" + inf="$tmpdir/$base.txt" + out="$outdir/$base.png" - if ! convert \ - "$jpg" \ - -colorspace RGB \ - -sigmoidal-contrast "$sigmoidal_contrast_trim" \ - -resize "$resize_percent%" \ - -colors "$colors" \ - -morphology "$morphology" "$morphology_shape" \ - "$tmp1" - then - warning "Could not create test image \"$tmp1\"; skipped." - rv=1 - continue - fi - - draw=() - v=255 - - while read -r primitive ; do - draw+=("-fill" "rgb($v,$v,$v)" "-draw" "$primitive") - v=$((v-2)) - done < <( - convert \ - "$tmp1" \ - -define connected-components:verbose=true \ - -define connected-components:exclude-header=true \ - -define connected-components:sort-order=increasing \ - -define connected-components:mean-color=true \ - -define connected-components:area-threshold="$area_treshold" \ - -virtual-pixel None \ - -connected-components "$connected_components" \ - -auto-level \ - "$com" | \ - perl -e ' - while(<>){ - chomp; - next unless /(\d+)x(\d+)\+(\d+)\+(\d+)/; - printf - "rectangle %d,%d,%d,%d\n", - $1, $2, $1+$3, $2+$4; - } - - ' | \ - sort | \ - uniq - ) - - if ! convert \ - "$tmp1" \ - -fill black -colorize 100 \ - "${draw[@]}" \ - "$tmp2" - then - warning "Could not convert \"$tmp1\" to \"$tmp2\"; skipped." - rv=1 - continue - elif ! convert \ - "$tmp2" \ - -trim info: \ - > "$inf" - then - warning "Could not determine trim info from \"$tmp2\"; skipped." - rv=1 - continue - fi - - if "$keep_tmpfiles" ; then if ! convert \ - "$tmp2" \ - -trim \ - "$trimmed" + "$jpg" \ + -colorspace RGB \ + -sigmoidal-contrast "$sigmoidal_contrast_trim" \ + -resize "$resize_percent%" \ + -colors "$colors" \ + -morphology "$morphology" "$morphology_shape" \ + "$tmp1" then - warning "Could not test trim \"$tmp2\"; skipped." + warning "Could not create test image \"$tmp1\"; skipped." rv=1 continue fi - fi - # upscale crop area from smaller temporary picture sizes - crop=$(simplify=$simplify perl -MEnv -e ' - while(<>) { - chomp(); - s/(\d+)/$1*${simplify}/eg; - /(\d+)x(\d+) \d+x\d+\+(\d+)\+(\d+)/ && do { - print $1."x".$2."+".$3."+".$4; + draw=() + v=255 + + while read -r primitive ; do + draw+=("-fill" "rgb($v,$v,$v)" "-draw" "$primitive") + v=$((v-2)) + done < <( + convert \ + "$tmp1" \ + -define connected-components:verbose=true \ + -define connected-components:exclude-header=true \ + -define connected-components:sort-order=increasing \ + -define connected-components:mean-color=true \ + -define connected-components:area-threshold="$area_treshold" \ + -virtual-pixel None \ + -connected-components "$connected_components" \ + -auto-level \ + "$com" | \ + perl -e ' + while(<>){ + chomp; + next unless /(\d+)x(\d+)\+(\d+)\+(\d+)/; + printf + "rectangle %d,%d,%d,%d\n", + $1, $2, $1+$3, $2+$4; + } + + ' | \ + sort | \ + uniq + ) + + if ! convert \ + "$tmp1" \ + -fill black -colorize 100 \ + "${draw[@]}" \ + "$tmp2" + then + warning "Could not convert \"$tmp1\" to \"$tmp2\"; skipped." + rv=1 + continue + elif ! convert \ + "$tmp2" \ + -trim info: \ + > "$inf" + then + warning "Could not determine trim info from \"$tmp2\"; skipped." + rv=1 + continue + fi + + if "$keep_tmpfiles" ; then + if ! convert \ + "$tmp2" \ + -trim \ + "$trimmed" + then + warning "Could not test trim \"$tmp2\"; skipped." + rv=1 + continue + fi + fi + + # upscale crop area from smaller temporary picture sizes + crop=$(simplify=$simplify perl -MEnv -e ' + while(<>) { + chomp(); + s/(\d+)/$1*${simplify}/eg; + /(\d+)x(\d+) \d+x\d+\+(\d+)\+(\d+)/ && do { + print $1."x".$2."+".$3."+".$4; + } } - } - ' < "$inf") + ' < "$inf") - info "Processing $base: crop=$crop ..." + info "Processing $base: crop=$crop ..." - if ! convert \ - "$jpg" \ - -crop "$crop" \ - -sigmoidal-contrast "$sigmoidal_contrast" \ - +dither -posterize "$posterize" \ - "$out" - then - warning "Could not process \"$jpg\"; skipped." - rv=1 - continue - fi -done + if ! convert \ + "$jpg" \ + -crop "$crop" \ + -sigmoidal-contrast "$sigmoidal_contrast" \ + +dither -posterize "$posterize" \ + "$out" + then + warning "Could not process \"$jpg\"; skipped." + rv=1 + continue + fi + done +fi # !direct if [[ $rv -ne 0 ]] ; then error "One or more errors during image conversion; aborted." "$rv" @@ -437,13 +458,15 @@ while read -r pdf ; do input+=("$pdf") done < <(ls -- "$tmpdir/"*.backdropped.pdf) +result_pdf=$(readlink -f "$result_filename") + if ! \ qpdf \ --empty \ --pages "${input[@]}" \ - -- "$result_filename" + -- "$result_pdf" then - warning "Could not concatenate \"$result_filename\"." + warning "Could not concatenate \"$result_pdf\"." rv=1 elif ! "$keep_tmpfiles" ; then if ! rm -f -- "$tmp_base" ; then