add option to not post-process pictures

This commit is contained in:
Tilman Kranz 2024-07-15 08:10:04 +02:00
parent dc4c5498b2
commit 822b984678

View File

@ -48,7 +48,7 @@ sigmoidal_contrast="45,50%"
posterize=6 posterize=6
# Keep intermediate images after processing # Keep intermediate images after processing
keep_tmpfiles=true keep_tmpfiles=false
# Paper size of all pages # Paper size of all pages
paper="a4paper" paper="a4paper"
@ -111,8 +111,14 @@ Arguments
--------- ---------
* `DIR`: Directory containing the original photos. * `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 * `--simplify INT`: Before morphology analysis, reduce
image dimensions by this factor image dimensions by this factor
(default: 2). (default: 2).
@ -148,6 +154,7 @@ EOF
## ##
# Arguments # Arguments
direct=false
i=1 i=1
options_done=false options_done=false
@ -160,6 +167,11 @@ while [[ $i -le $# ]] ; do
print_help print_help
exit 0 exit 0
;; ;;
--direct)
direct=true
i=$((i+1))
continue
;;
--simplify| \ --simplify| \
--morphology| \ --morphology| \
--morphology-shape| \ --morphology-shape| \
@ -262,117 +274,126 @@ if [[ $rv -ne 0 ]] ; then
error "One or more errors during input file naming; aborted." "$rv" error "One or more errors during input file naming; aborted." "$rv"
fi fi
for jpg in "$indir/"*.jpg ; do if "$direct" ; then
base=$(basename "$jpg" .jpg) for jpg in "$indir/"*.jpg ; do
tmp1="$tmpdir/$base.tmp1.png" base=$(basename "$jpg" .jpg)
tmp2="$tmpdir/$base.tmp2.png" out="$outdir/$base.png"
com="$tmpdir/$base.com.png" info "Resizing $jpg to $out ..."
trimmed="$tmpdir/$base.trimmed.png" convert -resize 1024 -dither Riemersma -colors 24 "$jpg" "$out"
inf="$tmpdir/$base.txt" done
out="$outdir/$base.png" 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 \ if ! convert \
"$tmp2" \ "$jpg" \
-trim \ -colorspace RGB \
"$trimmed" -sigmoidal-contrast "$sigmoidal_contrast_trim" \
-resize "$resize_percent%" \
-colors "$colors" \
-morphology "$morphology" "$morphology_shape" \
"$tmp1"
then then
warning "Could not test trim \"$tmp2\"; skipped." warning "Could not create test image \"$tmp1\"; skipped."
rv=1 rv=1
continue continue
fi fi
fi
# upscale crop area from smaller temporary picture sizes draw=()
crop=$(simplify=$simplify perl -MEnv -e ' v=255
while(<>) {
chomp(); while read -r primitive ; do
s/(\d+)/$1*${simplify}/eg; draw+=("-fill" "rgb($v,$v,$v)" "-draw" "$primitive")
/(\d+)x(\d+) \d+x\d+\+(\d+)\+(\d+)/ && do { v=$((v-2))
print $1."x".$2."+".$3."+".$4; 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 \ if ! convert \
"$jpg" \ "$jpg" \
-crop "$crop" \ -crop "$crop" \
-sigmoidal-contrast "$sigmoidal_contrast" \ -sigmoidal-contrast "$sigmoidal_contrast" \
+dither -posterize "$posterize" \ +dither -posterize "$posterize" \
"$out" "$out"
then then
warning "Could not process \"$jpg\"; skipped." warning "Could not process \"$jpg\"; skipped."
rv=1 rv=1
continue continue
fi fi
done done
fi # !direct
if [[ $rv -ne 0 ]] ; then if [[ $rv -ne 0 ]] ; then
error "One or more errors during image conversion; aborted." "$rv" error "One or more errors during image conversion; aborted." "$rv"
@ -437,13 +458,15 @@ while read -r pdf ; do
input+=("$pdf") input+=("$pdf")
done < <(ls -- "$tmpdir/"*.backdropped.pdf) done < <(ls -- "$tmpdir/"*.backdropped.pdf)
result_pdf=$(readlink -f "$result_filename")
if ! \ if ! \
qpdf \ qpdf \
--empty \ --empty \
--pages "${input[@]}" \ --pages "${input[@]}" \
-- "$result_filename" -- "$result_pdf"
then then
warning "Could not concatenate \"$result_filename\"." warning "Could not concatenate \"$result_pdf\"."
rv=1 rv=1
elif ! "$keep_tmpfiles" ; then elif ! "$keep_tmpfiles" ; then
if ! rm -f -- "$tmp_base" ; then if ! rm -f -- "$tmp_base" ; then