Update docs, various fixes.
[dja/scandal.git] / scan2pages.sh
1 #!/bin/bash
2
3 function usage {
4 cat >&2 << __EOF__ 
5 Usage: $0 [-vfpsmutb] [-D DPI] [-d depth] [-F format] 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/gray.
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         -e extension: use the output format signified by the extension. Default is png
14         -b: Optimize for BeBook One.
15         -1: each physical page contains one logical page
16         -2: each physical page contains two logical pages
17                 (neither specified: try to figure it out)
18 __EOF__
19 }
20
21
22 ## Setup and utilities ##
23 source $(dirname $0)/my_seq.sh
24
25 ## Process flags ##
26 convertflags=
27 unpaperflags=
28 depthset=
29 depth=
30 dpi=
31 skipmask=
32 verbose=
33 extension="png"
34 bebook=
35 logperphys=
36 while getopts 'vd:D:e:b12' OPTION
37 do
38         case $OPTION in
39         v)      verbose=1
40                 convertflags="$convertflags -verbose"
41                 unpaperflags="$unpaperflags -v --time"
42                 ;;
43         d)      depth="$OPTARG"
44                 ;;
45         D)      dpi=$OPTARG
46                 ;;
47         e)      extension="$OPTARG"
48                 ;;
49         b)      bebook=1
50                 ;;
51         1)      logperphys=1
52                 ;;
53         2)      logperphys=2
54                 ;;
55         ?)      usage
56                 exit 2
57                 ;;
58         esac
59 done
60 shift $(($OPTIND - 1))
61
62 # check we have an input and output!
63 if [[ $# != 2 ]]; then
64         echo "Wrong number of parameters (2 required, $# given: [$@])" >&2
65         usage
66         exit 2
67 fi
68
69 scanfile=$1
70 outdir=$2
71
72 filedir=$(dirname $1)
73 base=$(basename $1 .pdf)
74
75 # make the output dir
76 mkdir -p $outdir
77
78
79 # guess at DPI, unless it's been set
80 # the scans do not seem to accurately reflect their resolution.
81 if [[ "$dpi" = "" ]]; then
82         # %k gives the number of colours/shades in the image.
83         # 2 for b/w, and the scanner seems to default to 400 dpi for that
84         # 256 for greyscale, which has to be selected manually (so I'm assuming 300 for no good reason)
85         # 31564 for colour, which defaults to 300
86         if [[ $( convert $scanfile[0] -format "%k" info: || exit 1 ) == 2 ]]; then
87                 dpi=400;
88         else
89                 dpi=300
90         fi
91         if [[ $verbose == 1 ]]; then
92                 echo "DPI guessed at $dpi."
93         fi
94 fi
95
96 # guess at depth, unless it's been set
97 if [[ "$depth" = "" ]]; then
98         if [[ $( convert $scanfile[0] -format "%k" info: || exit 1 ) == 2 ]]; then
99                 depth=1;
100         else
101                 depth=8
102         fi
103         if [[ $verbose == 1 ]]; then
104                 echo "Depth guessed at $depth."
105         fi
106 fi
107
108 # figure out the number of pages
109 dscname=$outdir/${base}.dsc
110 pdf2dsc $scanfile $dscname || exit 1
111 pages=$(awk '$1 ~ "%%Pages" {print $2}' $dscname)
112 echo "Got $pages page(s)."
113 rm $dscname
114
115 # process pages 1 by 1 to avoid convert gobbling all the memory
116 for scanpgnum in `$my_seq 1 $pages`; do
117         echo "Processing page $scanpgnum."
118         
119         scanpgnum=$(printf '%03d' $scanpgnum)
120         
121         # convert from pdf
122         scanpg=$outdir/scanpg-${scanpgnum}.png
123         if [ ! -e $scanpg ]; then
124                 convert $convertflags -depth $depth -density $dpi $scanfile[$(expr $scanpgnum - 1)] \
125                         $scanpg || exit 1
126         fi;
127         
128 done;
129
130 # do ocr binarise
131 [ -e $outdir/scanpgs ] && rm -r $outdir/scanpgs
132 ocropus book2pages $outdir/scanpgs $outdir/scanpg-*.png || exit 1
133
134 for scanpgnum in  `$my_seq 1 $pages`; do
135
136         binscanpg=$outdir/scanpgs/$(printf '%04d' $scanpgnum).bin.png
137         scanpgnum=$(printf '%03d' $scanpgnum)
138         scanpg=$outdir/scanpg-${scanpgnum}.png
139
140         # preprocess scanned page
141         cleanscanpg=$outdir/scanpg-clean-${scanpgnum}.pnm
142         if [ ! -e $cleanscanpg ]; then
143                 # create mask: 
144                 # ... downscale, blur,
145                 convert $convertflags -resize 10% -depth 8 -blur 10 -median 2 $scanpg $outdir/scanpg-mask-${scanpgnum}.png ||exit 1
146
147                 # ... get crop co-ords. They're off by ~2 as I don't know how to
148                 # properly correct for the border.
149                 cropcords=$(convert -border 1x1 -bordercolor '#000' -resize 1000% -trim -fuzz 90% -format "%wx%h%O" $outdir/scanpg-mask-${scanpgnum}.png info: || exit 1)
150                 
151                 # ... crop and despeckle? the final pre-prepared image
152                 convert $convertflags -crop $cropcords $binscanpg  $cleanscanpg || exit 1
153         fi;
154         
155         # check it hasn't mostly disappeared - e.g. if the scan was all black
156         # (e.g. forgot to put the book down when you first hit scan)
157         if [[ $(convert  $cleanscanpg -format '%[fx:s.w*s.h>1000]' info:) = "0" ]]; then
158                 [[ $verbose == 1 ]] && echo "Discarding pg ${scanpgnum}: not enough remains after masking."
159                 continue; 
160         fi;
161         
162         #unpaper it
163         physpgbase=$outdir/physpg-${scanpgnum}
164         if [ ! -e ${physpgbase}-1.pnm ] || [ ! -e ${physpgbase}-2.pnm ]; then
165                 unpaper $unpaperflags --layout double --overwrite -ni 10 -op 2 $cleanscanpg ${physpgbase}-%01d.pnm || exit 1
166         fi;
167         
168         for physpgnum in $($my_seq 1 2); do
169                 echo Processing physical page ${physpgnum}.
170                 
171                 physpg=${physpgbase}-${physpgnum}.pnm #$physpgbase has $outdir/ already
172                 
173                 cleanphyspg=$outdir/physpg-clean-${scanpgnum}-${physpgnum}.pnm
174                 
175                 if [ ! -e $cleanphyspg ]; then
176                         # remask and retrim. FIXME: may throw off 2page detection
177                         convert $convertflags -resize 10% -depth 8 -gamma 0.01 -median 2 $physpg $outdir/physpg-mask-${scanpgnum}-${physpgnum}.png ||exit 1
178
179                         # Trim #-border 1x1 -bordercolor '#fff' -trim -fuzz 30% 
180                         cropcords=$(convert -trim -fuzz 90%\
181                          -resize 1000% -format "%wx%h%O" $outdir/physpg-mask-${scanpgnum}-${physpgnum}.png info: || exit 1)
182                 
183                         # ... crop and despeckle? the final pre-prepared image
184                         convert $convertflags -crop $cropcords $physpg $cleanphyspg || exit 1
185                 fi;
186                 
187                 #detect if the page is 2-up
188
189                 if [[ $logperphys == 2 ]] || ( [[ $logperphys != 1 ]] && $(dirname $0)/detect2pages.sh ${cleanphyspg} ${scanpgnum} ${physpgnum} ); then
190                         if [[ $verbose == 1 ]]; then
191                                 echo "Resplitting physical page ${physpgnum}."
192                         fi
193                         unpaper $unpaperflags --pre-rotate 90 --layout double --overwrite \
194                          -op 2 --no-blackfilter --no-grayfilter --no-noisefilter \
195                          --no-blurfilter $cleanphyspg $outdir/logpg-${scanpgnum}-${physpgnum}-%01d.pnm || exit 1
196                 else
197                         cp $cleanphyspg $outdir/logpg-${scanpgnum}-${physpgnum}-1.pnm
198                 fi;
199
200                 #prepare for ocr
201                 convert $convertflags $outdir/logpg-${scanpgnum}-${physpgnum}-?.pnm $outdir/logpg-${scanpgnum}-${physpgnum}-%01d.png || exit 1
202
203                 #final convert and clean w/ bebook optimisation
204                 if [[ $bebook ]]; then
205                         convert $convertflags $outdir/logpg-${scanpgnum}-${physpgnum}-?.pnm -trim -fuzz 80% -resize 1200x1600 $outdir/final-${scanpgnum}-${physpgnum}-%01d.${extension} || exit 1
206                 else
207                         convert $convertflags $outdir/logpg-${scanpgnum}-${physpgnum}-?.pnm $outdir/final-${scanpgnum}-${physpgnum}-%01d.${extension} || exit 1
208                 fi
209
210         done;
211
212 done
213
214 #try full ocr
215 rm -rf $outdir/logpgs
216 ocropus book2pages $outdir/logpgs $outdir/logpg-*.png
217 ocropus pages2lines $outdir/logpgs
218 ocropus lines2fsts $outdir/logpgs
219 ocropus fsts2bestpaths $outdir/logpgs
220 ocropus buildhtml $outdir/logpgs > $outdir/out.html
221
222 mkdir -p $outdir/pages
223 mv $outdir/final-*.${extension} $outdir/pages
224         

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