3 UNPAPER_PATH="/Users/dja/Applications/unpaper-0.3/"
4 PATH=$UNPAPER_PATH:$PATH
8 Usage: $0 [-vfpsmut] [-d depth] pdffile outdir
9 Convert pdffile - a pdf of scanned facing pages - to a set of images in outdir,
10 each with only one page.
13 -d depth: Use given depth. Default is 1. Unpaper can only handle up to 8.
14 -s: skip masking/trimming. Overrides -m.
15 -f: Forceably redo everything.
16 -p: Forceably redo pdf conversion. Implies options below, equivalent to -f.
17 -m: Forceably redo masking/trimming and other preprocessing. Impiles options below.
18 -u: Forceably redo unpaper processing. Implies option below. IGNORED
19 -t: Forceably redo final trimming and cleaning. IGNORED
24 # process for -f flag to forceably redo all conversions
33 while getopts 'vd:sfpmuc' OPTION
36 v) convertflags="$convertflags -verbose"
37 unpaperflags="$unpaperflags -v --time"
39 d) depthflags="-depth $OPTARG"
43 f) forcepdf=1; forcemask=1; forceunpaper=1; forceclean=1
45 p) forcepdf=1; forcemask=1; forceunpaper=1; forceclean=1
47 m) forcemask=1; forceunpaper=1; forceclean=1
49 u) forceunpaper=1; forceclean=1
61 shift $(($OPTIND - 1))
63 # check we have an input and output!
64 if [[ $# != 2 ]]; then
65 echo "Wrong number of parameters (2 required, $# given: [$@])" >&2
74 base=$(basename $1 .pdf)
79 # figure out the number of pages
80 dscname=$dir/${base}.dsc
81 pdf2dsc $file $dscname || exit 1
82 pages=$(awk '$1 ~ "%%Pages" {print $2}' $dscname)
83 echo "Got $pages page(s)."
86 # process pages 1 by 1 to avoid convert gobbling all the memory
87 for pg in `jot - 1 $pages`; do
88 echo "Processing page $pg."
90 pgn=$(printf '%03d' $pg)
93 origpnm=$dir/pg-${pgn}.pnm
94 if [[ ! $([ -e $origpnm ]) || $forcepdf ]]; then
95 convert $convertflags $depthflags -density 300 $file[$(expr $pg - 1)] \
100 preppnm=$dir/pg-pp-${pgn}.pnm
101 if [[ ! $skipmask && ( ! $([ -e $preppnm ]) || $forcemask ) ]]; then
103 # ... downscale, blur,
104 convert $convertflags -resize 25% -depth 8 -blur 10 $origpnm $dir/pg-mask-${pgn}.pnm ||exit 1
106 # ... get crop co-ords
107 cropcords=$(convert -resize 400% -trim -fuzz 90% -format "%wx%h%O" $dir/pg-mask-${pgn}.pnm info: || exit 1)
109 # ... crop and despeckle? the final pre-prepared image
110 convert $convertflags -crop $cropcords $origpnm $preppnm || exit 1
111 elif [[ $skipmask ]]; then
116 #names go a bit funny here
117 #also, ignore flags starting here
118 unppnm=$dir/upg-${pgn}-%01d.pnm
119 unpaper $unpaperflags --layout double --overwrite --no-blackfilter -ni 10 -op 2 $preppnm $unppnm || exit 1
121 # final convert and clean
122 convert $convertflags $dir/upg-${pgn}-?.pnm $dir/upg-${pgn}-%01d.tiff || exit 1
128 mv $dir/upg-*.tiff $dir/pages