Update Updated forms of the scripts are now hosted as gists
[1] [2]
Based on the answer my matthewd I have written scripts that automate the process as intended for scans with a reasonable contrast. The scripts use poppler's pdfimages, ImageMagick's convert and pdftk.
imagemagick-scan-pdf-to-mono.sh (depends on the second script; Output might be rotated which can be fixed by running pdftk FILE.pdf cat 1-endW output OUT.pdf. Rotation direction can be changed by using 1-endE instead of 1-endW)
#!/usr/bin/env bash
# -*- mode: sh; coding: us-ascii-unix -*-
# source libstacktrace || true
# set -e -u -E
MANUAL="
Usage: $0 [options] INPUT OUTPUT
Converts a scan-pdf (assuming one image per page) to monochrome.
-f INT, --from-page INT
Process only pages with page number >= INT
-t INT, --to-page INT
Process only pages with page number <= INT
-P, --parallel INT
Process INT pages in parallel each.
-v, --verbose / +v, --noverbose
Enables/Disables verbose reporting.
-h, -?, --help
Prints this message
"
vecho(){ $VERBOSE && echo "$@"; }
######### COMMAND LINE PARSING #######################################
declare VERBOSE=false
declare ARGS=()
declare PAGE_LIMIT_LOW=""
declare PAGE_LIMIT_HIGH=""
declare PARALLEL=1
## Print manual
if [[ $# -eq 0 ]]; then
echo "$MANUAL"
exit 1
fi
## Getopt-style consumption of arguments ##
##
## Don't forget "shift", don't delete "--" and "*" cases.
while [[ $# -gt 0 ]]; do
case "$1" in
-h|-\?|--help)
echo "$MANUAL"
exit 0
shift ;;
-v|--verbose)
VERBOSE=true
shift ;;
+v|--no-verbose)
VERBOSE=false
shift ;;
-f|--from-page)
PAGE_LIMIT_LOW="-f $2"
shift 2 ;;
-t|--to-page)
PAGE_LIMIT_HIGH="-l $2"
shift 2 ;;
-P|--parallel)
PARALLEL=$2
shift 2 ;;
--)
shift
break ;;
*)
ARGS[${#ARGS[@]}]="$1"
shift ;;
esac
done
## Consume stuff remaining after -- ##
while [[ $# -gt 0 ]]; do
ARGS[${#ARGS[@]}]="$1"
shift
done
## Note that ${ARGS[@]} is considered unbound if it is empty!
INFILE=$(readlink -m "${ARGS[0]}")
OUTFILE=$(readlink -m "${ARGS[1]}")
TMPDIR=$(mktemp -d)
vecho "Using work directory '$TMPDIR'."
cd "$TMPDIR"
vecho "Extracting images from '$INFILE'..."
## Cannot be parallelized, file-locking issue.
cmd="pdfimages -j $PAGE_LIMIT_LOW $PAGE_LIMIT_HIGH $(printf %q "$INFILE") page"
# vecho "$cmd"
eval "$cmd" || true
find -name "page-*" -and -not -name "page-*-mono*" \
| xargs -P $PARALLEL -I FILE sh -c "
imagemagick-scan-to-mono.sh FILE FILE-mono.pdf \
&& { if $VERBOSE; then echo Finished file 'FILE'; fi; }
rm FILE
"
vecho "Assembling PDF file '$OUTFILE'..."
pdftk page-*-mono.pdf cat output out.pdf
mv out.pdf "$OUTFILE"
rm page-*-mono.pdf
rmdir "$TMPDIR" || ls -l
imagemagick-scan-to-mono.sh
#!/usr/bin/env bash
# -*- mode: sh; coding: us-ascii-unix -*-
source libstacktrace || true
set -e -u -E
MANUAL="
Usage: $0 INFILE OUTFILE
Takes a document scan INFILE (an image) and produces a monochromatized
output file version.
"
if [[ "${1:-}" = "-?" ]] || [[ "${1:-}" = "-h" ]] || [[ "${1:-}" = "--help" ]]; then
echo "$MANUAL"
exit 0
fi
BLURRADIUS="20"
INFILE="$(readlink -m "$1")"
OUTFILE="$(readlink -m "$2")"
TMPDIR="$(mktemp -d)"
cd "$TMPDIR"
convert "$INFILE" -colorspace Gray 01.png
convert 01.png -blur "${BLURRADIUS}x${BLURRADIUS}" 02.tif
convert 01.png 02.tif -compose Divide_Src -composite 03.tif
convert 03.tif -threshold 90% -type bilevel -compress group4 "$OUTFILE"
rm 01.png 02.tif 03.tif
rmdir "$TMPDIR"