Skip to content

Instantly share code, notes, and snippets.

@peterjurkovic
Created January 19, 2017 16:04
Show Gist options
  • Save peterjurkovic/03d894c33d2b263b8f66366457769fef to your computer and use it in GitHub Desktop.
Save peterjurkovic/03d894c33d2b263b8f66366457769fef to your computer and use it in GitHub Desktop.
#!/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"
# for file in *.jpg; do ./imagemagick-scan-to-mono.sh $file rotated-$file; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment