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 ##
21 # Mac OS X doesn't have seq. It has jot instead.
24 if [[ $(uname) == "Darwin" ]]; then my_seq=$Darwin_seq;
25 else my_seq=$Linux_seq;
38 while getopts 'vd:sfpmuc' OPTION
41 v) convertflags="$convertflags -verbose"
42 unpaperflags="$unpaperflags -v --time"
44 d) depthflags="-depth $OPTARG"
48 f) forcepdf=1; forcemask=1; forceunpaper=1; forceclean=1
50 p) forcepdf=1; forcemask=1; forceunpaper=1; forceclean=1
52 m) forcemask=1; forceunpaper=1; forceclean=1
54 u) forceunpaper=1; forceclean=1
66 shift $(($OPTIND - 1))
68 # check we have an input and output!
69 if [[ $# != 2 ]]; then
70 echo "Wrong number of parameters (2 required, $# given: [$@])" >&2
79 base=$(basename $1 .pdf)
84 # figure out the number of pages
85 dscname=$dir/${base}.dsc
86 pdf2dsc $file $dscname || exit 1
87 pages=$(awk '$1 ~ "%%Pages" {print $2}' $dscname)
88 echo "Got $pages page(s)."
91 # process pages 1 by 1 to avoid convert gobbling all the memory
92 for pg in `$my_seq 1 $pages`; do
93 echo "Processing page $pg."
95 pgn=$(printf '%03d' $pg)
98 origpnm=$dir/pg-${pgn}.pnm
99 if [[ ! $([ -e $origpnm ]) || $forcepdf ]]; then
100 convert $convertflags $depthflags -density 300 $file[$(expr $pg - 1)] \
105 preppnm=$dir/pg-pp-${pgn}.pnm
106 if [[ ! $skipmask && ( ! $([ -e $preppnm ]) || $forcemask ) ]]; then
108 # ... downscale, blur,
109 convert $convertflags -resize 25% -depth 8 -blur 10 $origpnm $dir/pg-mask-${pgn}.pnm ||exit 1
111 # ... get crop co-ords. They're off by ~2 as I don't know how to
112 # properly correct for the border.
113 cropcords=$(convert -border 1x1 -bordercolor '#000' -resize 400% -trim -fuzz 90% -format "%wx%h%O" $dir/pg-mask-${pgn}.pnm info: || exit 1)
115 # ... crop and despeckle? the final pre-prepared image
116 convert $convertflags -crop $cropcords $origpnm $preppnm || exit 1
117 elif [[ $skipmask ]]; then
122 #names go a bit funny here
123 #also, ignore flags starting here
124 unppnm=$dir/upg-${pgn}-%01d.pnm
125 unpaper $unpaperflags --layout double --overwrite --no-blackfilter -ni 10 -op 2 $preppnm $unppnm || exit 1
127 # final convert and clean
128 convert $convertflags $dir/upg-${pgn}-?.pnm $dir/upg-${pgn}-%01d.tiff || exit 1
134 mv $dir/upg-*.tiff $dir/pages