249f2ad17eac469cdf9e8dff1723e3d245e7c6c4
[dja/scandal.git] / scan2pages.sh
1 #!/bin/bash
2
3 function usage {
4 cat >&2 << __EOF__ 
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.
8 OPTIONS:
9         -v: Be verbose.
10         -d depth: Use given depth. Default is 1 for black and white, 8 for colour.
11                 Unpaper can only handle up to 8.
12         -D dpi: Use given DPI. Colour scans on library scanners are usually 300, bw 400.
13         -F extension: use the output format signified by the extension. Default is png
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
20 __EOF__
21 }
22
23 ## Setup and utilities ##
24 # Mac OS X doesn't have seq. It has jot instead.
25 Linux_seq="seq"
26 Darwin_seq="jot -"
27 if [[ $(uname) == "Darwin" ]]; then my_seq=$Darwin_seq;
28 else my_seq=$Linux_seq;
29 fi;
30
31
32 ## Process flags ##
33 forcepdf=
34 forcemask=
35 forceunpaper=
36 forceclean=
37 convertflags=
38 unpaperflags=
39 depthset=
40 depth=
41 dpi=
42 skipmask=
43 verbose=
44 extension="png"
45 while getopts 'vd:sfpmucF:' OPTION
46 do
47         case $OPTION in
48         v)      verbose=1
49                 convertflags="$convertflags -verbose"
50                 unpaperflags="$unpaperflags -v --time"
51                 ;;
52         d)      depth="$OPTARG"
53                 ;;
54         D)      dpi=$OPTARG
55                 ;;
56         s)      skipmask=1;
57                 ;;
58         f)      forcepdf=1; forcemask=1; forceunpaper=1; forceclean=1
59                 ;;
60         p)      forcepdf=1; forcemask=1; forceunpaper=1; forceclean=1
61                 ;;
62         m)      forcemask=1; forceunpaper=1; forceclean=1
63                 ;;
64         u)      forceunpaper=1; forceclean=1
65                 ;;
66         t)      forceclean=1
67                 ;;
68         F)      extension="$OPTARG"
69                 ;;
70         ?)      usage
71                 exit 2
72                 ;;
73         esac
74 done
75 shift $(($OPTIND - 1))
76
77 # check we have an input and output!
78 if [[ $# != 2 ]]; then
79         echo "Wrong number of parameters (2 required, $# given: [$@])" >&2
80         usage
81         exit 2
82 fi
83
84 file=$1
85 dir=$2
86
87 filedir=$(dirname $1)
88 base=$(basename $1 .pdf)
89
90 # make the output dir
91 mkdir -p $dir
92
93 # figure out the number of pages
94 dscname=$dir/${base}.dsc
95 pdf2dsc $file $dscname || exit 1
96 pages=$(awk '$1 ~ "%%Pages" {print $2}' $dscname)
97 echo "Got $pages page(s)."
98 rm $dscname
99
100 # guess at DPI, unless it's been set
101 # the scans do not seem to accurately reflect their resolution.
102 if [[ "$dpi" = "" ]]; then
103         # %k gives the number of colours/shades in the image.
104         # 2 for b/w, and the scanner seems to default to 400 dpi for that
105         # 256 for greyscale, which has to be selected manually (so I'm assuming 300 for no good reason)
106         # 31564 for colour, which defaults to 300
107         if [[ $( convert $file[0] -format "%k" info: || exit 1 ) == 2 ]]; then
108                 dpi=400;
109         else
110                 dpi=300
111         fi
112         if [[ $verbose == 1 ]]; then
113                 echo "DPI guessed at $dpi."
114         fi
115 fi
116
117 # guess at depth, unless it's been set
118 if [[ "$depth" = "" ]]; then
119         if [[ $( convert $file[0] -format "%k" info: || exit 1 ) == 2 ]]; then
120                 depth=1;
121         else
122                 depth=8
123         fi
124         if [[ $verbose == 1 ]]; then
125                 echo "Depth guessed at $depth."
126         fi
127 fi
128
129 # process pages 1 by 1 to avoid convert gobbling all the memory
130 for pg in `$my_seq 1 $pages`; do
131         echo "Processing page $pg."
132         
133         pgn=$(printf '%03d' $pg)
134         
135         # convert from pdf
136         origpnm=$dir/pg-${pgn}.pnm
137         if [[ ! $([ -e $origpnm ]) || $forcepdf ]]; then
138                 convert $convertflags -depth $depth -density $dpi $file[$(expr $pg - 1)] \
139                         $origpnm || exit 1
140         fi;
141         
142         # preprocess it!
143         preppnm=$dir/pg-pp-${pgn}.pnm
144         if [[ ! $skipmask && ( ! $([ -e $preppnm ]) || $forcemask ) ]]; then
145                 # create mask: 
146                 # ... downscale, blur,
147                 convert $convertflags -resize 25% -depth 8 -blur 10 $origpnm $dir/pg-mask-${pgn}.pnm ||exit 1
148
149                 # ... get crop co-ords. They're off by ~2 as I don't know how to
150                 # properly correct for the border.
151                 cropcords=$(convert -border 1x1 -bordercolor '#000' -resize 400% -trim -fuzz 90% -format "%wx%h%O" $dir/pg-mask-${pgn}.pnm info: || exit 1)
152                 
153                 # ... crop and despeckle? the final pre-prepared image
154                 convert $convertflags -crop $cropcords $origpnm $preppnm || exit 1
155         elif [[ $skipmask ]]; then
156                 cp $origpnm $preppnm
157         fi;
158         
159         #unpaper it
160         #names go a bit funny here
161         #also, ignore flags starting here
162         unppnm=$dir/upg-${pgn}-%01d.pnm
163         unpaper $unpaperflags --layout double --overwrite --no-blackfilter -ni 10 -op 2 $preppnm $unppnm || exit 1
164
165         # final convert and clean
166         convert $convertflags $dir/upg-${pgn}-?.pnm $dir/upg-${pgn}-%01d.${extension} || exit 1
167         
168         
169 done
170
171 mkdir $dir/pages
172 mv $dir/upg-*.${extension} $dir/pages
173         

UCC git Repository :: git.ucc.asn.au