commandline parser
This commit is contained in:
parent
833dbc798c
commit
77f79038da
163
photos2pdf
163
photos2pdf
@ -28,8 +28,7 @@ sigmoidal_contrast="45,50%"
|
|||||||
# Number of colors result images are quantized to
|
# Number of colors result images are quantized to
|
||||||
posterize=6
|
posterize=6
|
||||||
|
|
||||||
# Keep intermediate images after processing (note that they will be removed in
|
# Keep intermediate images after processing
|
||||||
# any case at the next run of this script)
|
|
||||||
keep_tmpfiles=true
|
keep_tmpfiles=true
|
||||||
|
|
||||||
# Paper size of all pages
|
# Paper size of all pages
|
||||||
@ -42,10 +41,13 @@ offset="1cm 3cm"
|
|||||||
scale=0.75
|
scale=0.75
|
||||||
|
|
||||||
# PDF backdrop template directory
|
# PDF backdrop template directory
|
||||||
templatedir=/var/lib/photos2pdf/templates
|
templatedir=/var/lib/photos2pdf/template
|
||||||
|
|
||||||
# Filename of complete result PDF
|
# Filename of complete result PDF
|
||||||
result="pdf/result.pdf"
|
result_filename="result.pdf"
|
||||||
|
|
||||||
|
# Directory of original files to process (initially unset)
|
||||||
|
dir=
|
||||||
|
|
||||||
##
|
##
|
||||||
# Functions
|
# Functions
|
||||||
@ -71,26 +73,127 @@ info() {
|
|||||||
log INFO "$1"
|
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
|
# Arguments
|
||||||
|
|
||||||
dir=$1
|
i=1
|
||||||
|
options_done=false
|
||||||
|
|
||||||
if [[ -z $dir ]] ; then
|
while [[ $i -le $# ]] ; do
|
||||||
error "Usage: $0 DIR"
|
arg=$(eval "echo \$$i")
|
||||||
fi
|
|
||||||
|
|
||||||
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
|
case "$arg" in
|
||||||
error "Directory not found: $dir ($origdir)"
|
*)
|
||||||
fi
|
[[ -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
|
# Main Program
|
||||||
|
|
||||||
job_id=$(printf "%s" "$origdir" | sha256sum | cut -d" " -f1)
|
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
|
if [[ -n $TMP ]] ; then
|
||||||
tmp_base=$TMP/photos2pdf/$job_id
|
tmp_base=$TMP/photos2pdf/$job_id
|
||||||
elif [[ -n $XDG_RUNTIME_DIR ]] ; then
|
elif [[ -n $XDG_RUNTIME_DIR ]] ; then
|
||||||
@ -99,10 +202,6 @@ else
|
|||||||
tmp_base=/tmp/photos2pdf/$job_id
|
tmp_base=/tmp/photos2pdf/$job_id
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! mkdir -p "$tmp_base" ; then
|
|
||||||
error "Could not create temporary workign directory $tmp_base; aborted"
|
|
||||||
fi
|
|
||||||
|
|
||||||
indir="$tmp_base/in"
|
indir="$tmp_base/in"
|
||||||
tmpdir="$tmp_base/tmp"
|
tmpdir="$tmp_base/tmp"
|
||||||
outdir="$tmp_base/out"
|
outdir="$tmp_base/out"
|
||||||
@ -189,9 +288,6 @@ for jpg in "$indir/"*.jpg ; do
|
|||||||
while(<>){
|
while(<>){
|
||||||
chomp;
|
chomp;
|
||||||
next unless /(\d+)x(\d+)\+(\d+)\+(\d+)/;
|
next unless /(\d+)x(\d+)\+(\d+)\+(\d+)/;
|
||||||
printf
|
|
||||||
STDERR
|
|
||||||
"DEBUG: component: $_\n";
|
|
||||||
printf
|
printf
|
||||||
"rectangle %d,%d,%d,%d\n",
|
"rectangle %d,%d,%d,%d\n",
|
||||||
$1, $2, $1+$3, $2+$4;
|
$1, $2, $1+$3, $2+$4;
|
||||||
@ -202,12 +298,6 @@ for jpg in "$indir/"*.jpg ; do
|
|||||||
uniq
|
uniq
|
||||||
)
|
)
|
||||||
|
|
||||||
printf "DEBUG: connected components in \"%s\":" "$tmp1"
|
|
||||||
for d in "${draw[@]}" ; do
|
|
||||||
printf " \"%s\"" "$d"
|
|
||||||
done
|
|
||||||
printf "\n"
|
|
||||||
|
|
||||||
if ! convert \
|
if ! convert \
|
||||||
"$tmp1" \
|
"$tmp1" \
|
||||||
-fill black -colorize 100 \
|
-fill black -colorize 100 \
|
||||||
@ -250,13 +340,6 @@ for jpg in "$indir/"*.jpg ; do
|
|||||||
}
|
}
|
||||||
' < "$inf")
|
' < "$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 ..."
|
info "Processing $base: crop=$crop ..."
|
||||||
|
|
||||||
if ! convert \
|
if ! convert \
|
||||||
@ -307,7 +390,7 @@ for png in "out/"*.png ; do
|
|||||||
continue
|
continue
|
||||||
elif ! pdfjam \
|
elif ! pdfjam \
|
||||||
--quiet \
|
--quiet \
|
||||||
--outfile tmp \
|
--outfile "$tmpdir" \
|
||||||
--paper "$paper" \
|
--paper "$paper" \
|
||||||
"$jam_landscape" \
|
"$jam_landscape" \
|
||||||
--scale "$scale" \
|
--scale "$scale" \
|
||||||
@ -334,15 +417,17 @@ while read -r pdf ; do
|
|||||||
input+=("$pdf")
|
input+=("$pdf")
|
||||||
done < <(ls -- "$tmpdir/"*.backdropped.pdf)
|
done < <(ls -- "$tmpdir/"*.backdropped.pdf)
|
||||||
|
|
||||||
if ! pdftk "${input[@]}" cat output "$result" ; then
|
if ! pdftk "${input[@]}" cat output "$pdfdir/$result_filename" ; then
|
||||||
warning "Could not concatenate \"$result\"."
|
warning "Could not concatenate \"$result_filename\"."
|
||||||
rv=1
|
|
||||||
elif ! rm -f -- "tmp/"*.pdf ; then
|
|
||||||
warning "Could not remove tempfiles in \"$dir/tmp\"."
|
|
||||||
rv=1
|
rv=1
|
||||||
|
elif ! "$keep_tmpfiles" ; then
|
||||||
|
if ! rm -f -- "$tmp_base" ; then
|
||||||
|
warning "Could not remove temporary files."
|
||||||
|
rv=1
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $rv -ne 0 ]] ; then
|
if [[ $rv -ne 0 ]] ; then
|
||||||
error "One or more errors during PDF generation; aborted." "$rv"
|
error "One or more errors; aborted." "$rv"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user