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

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