5 Usage: $0 [-vfpsmut] [-d depth] pdffile outdir
6 Convert pdffile - a pdf of scanned facing pages - to a set of images in outdir,
7 each with only one page.
10 -d depth: Use given depth. Default is 1. Unpaper can only handle up to 8.
11 -s: skip masking/trimming. Overrides -m.
12 -f: Forceably redo everything.
13 -p: Forceably redo pdf conversion. Implies options below, equivalent to -f.
14 -m: Forceably redo masking/trimming and other preprocessing. Impiles options below.
15 -u: Forceably redo unpaper processing. Implies option below. IGNORED
16 -t: Forceably redo final trimming and cleaning. IGNORED
20 ## Setup and utilities ##
22 UNPAPER_PATH="/home/wheel/dja/public-html/scandal/unpaper-0.3/bin"
23 PATH=$UNPAPER_PATH:$PATH
25 # Mac OS X doesn't have seq. It has jot instead.
28 if [[ $(uname) == "Darwin" ]]; then my_seq=$Darwin_seq;
29 else my_seq=$Linux_seq;
42 while getopts 'vd:sfpmuc' OPTION
45 v) convertflags="$convertflags -verbose"
46 unpaperflags="$unpaperflags -v --time"
48 d) depthflags="-depth $OPTARG"
52 f) forcepdf=1; forcemask=1; forceunpaper=1; forceclean=1
54 p) forcepdf=1; forcemask=1; forceunpaper=1; forceclean=1
56 m) forcemask=1; forceunpaper=1; forceclean=1
58 u) forceunpaper=1; forceclean=1
70 shift $(($OPTIND - 1))
72 # check we have an input and output!
73 if [[ $# != 2 ]]; then
74 echo "Wrong number of parameters (2 required, $# given: [$@])" >&2
83 base=$(basename $1 .pdf)
88 # figure out the number of pages
89 dscname=$dir/${base}.dsc
90 pdf2dsc $file $dscname || exit 1
91 pages=$(awk '$1 ~ "%%Pages" {print $2}' $dscname)
92 echo "Got $pages page(s)."
95 # process pages 1 by 1 to avoid convert gobbling all the memory
96 for pg in `$my_seq 1 $pages`; do
97 echo "Processing page $pg."
99 pgn=$(printf '%03d' $pg)
102 origpnm=$dir/pg-${pgn}.pnm
103 if [[ ! $([ -e $origpnm ]) || $forcepdf ]]; then
104 convert $convertflags $depthflags -density 300 $file[$(expr $pg - 1)] \
109 preppnm=$dir/pg-pp-${pgn}.pnm
110 if [[ ! $skipmask && ( ! $([ -e $preppnm ]) || $forcemask ) ]]; then
112 # ... downscale, blur,
113 convert $convertflags -resize 25% -depth 8 -blur 10 $origpnm $dir/pg-mask-${pgn}.pnm ||exit 1
115 # ... get crop co-ords
116 cropcords=$(convert -resize 400% -trim -fuzz 90% -format "%wx%h%O" $dir/pg-mask-${pgn}.pnm info: || exit 1)
118 # ... crop and despeckle? the final pre-prepared image
119 convert $convertflags -crop $cropcords $origpnm $preppnm || exit 1
120 elif [[ $skipmask ]]; then
125 #names go a bit funny here
126 #also, ignore flags starting here
127 unppnm=$dir/upg-${pgn}-%01d.pnm
128 unpaper $unpaperflags --layout double --overwrite --no-blackfilter -ni 10 -op 2 $preppnm $unppnm || exit 1
130 # final convert and clean
131 convert $convertflags $dir/upg-${pgn}-?.pnm $dir/upg-${pgn}-%01d.tiff || exit 1
137 mv $dir/upg-*.tiff $dir/pages