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.
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)
22 ## Setup and utilities ##
23 source $(dirname $0)/my_seq.sh
36 while getopts 'vd:D:e:b12' OPTION
40 convertflags="$convertflags -verbose"
41 unpaperflags="$unpaperflags -v --time"
47 e) extension="$OPTARG"
60 shift $(($OPTIND - 1))
62 # check we have an input and output!
63 if [[ $# != 2 ]]; then
64 echo "Wrong number of parameters (2 required, $# given: [$@])" >&2
73 base=$(basename $1 .pdf)
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
91 if [[ $verbose == 1 ]]; then
92 echo "DPI guessed at $dpi."
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
103 if [[ $verbose == 1 ]]; then
104 echo "Depth guessed at $depth."
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)."
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."
119 scanpgnum=$(printf '%03d' $scanpgnum)
122 scanpg=$outdir/scanpg-${scanpgnum}.png
123 if [ ! -e $scanpg ]; then
124 convert $convertflags -depth $depth -density $dpi $scanfile[$(expr $scanpgnum - 1)] \
131 [ -e $outdir/scanpgs ] && rm -r $outdir/scanpgs
132 ocropus book2pages $outdir/scanpgs $outdir/scanpg-*.png || exit 1
134 for scanpgnum in `$my_seq 1 $pages`; do
136 binscanpg=$outdir/scanpgs/$(printf '%04d' $scanpgnum).bin.png
137 scanpgnum=$(printf '%03d' $scanpgnum)
138 scanpg=$outdir/scanpg-${scanpgnum}.png
140 # preprocess scanned page
141 cleanscanpg=$outdir/scanpg-clean-${scanpgnum}.pnm
142 if [ ! -e $cleanscanpg ]; then
144 # ... downscale, blur,
145 convert $convertflags -resize 10% -depth 8 -blur 10 -median 2 $scanpg $outdir/scanpg-mask-${scanpgnum}.png ||exit 1
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)
151 # ... crop and despeckle? the final pre-prepared image
152 convert $convertflags -crop $cropcords $binscanpg $cleanscanpg || exit 1
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."
163 physpgbase=$outdir/physpg-${scanpgnum}
164 if [ ! -e ${physpgbase}-1.pnm ] || [ ! -e ${physpgbase}-2.pnm ]; then
165 # most of unpaper's processing is redundant given ocropus, and somewhat too agressive
166 unpaper $unpaperflags --layout double --overwrite -op 2 \
168 $cleanscanpg ${physpgbase}-%01d.pnm || exit 1
172 for physpgnum in $($my_seq 1 2); do
173 echo Processing physical page ${physpgnum}.
175 physpg=${physpgbase}-${physpgnum}.pnm #$physpgbase has $outdir/ already
177 cleanphyspg=$outdir/physpg-clean-${scanpgnum}-${physpgnum}.pnm
179 if [ ! -e $cleanphyspg ]; then
180 # remask and retrim. FIXME: may throw off 2page detection
181 convert $convertflags -resize 10% -depth 8 -gamma 0.01 -median 2 $physpg $outdir/physpg-mask-${scanpgnum}-${physpgnum}.png ||exit 1
183 # Trim #-border 1x1 -bordercolor '#fff' -trim -fuzz 30%
184 cropcords=$(convert -trim -fuzz 90%\
185 -resize 1000% -format "%wx%h%O" $outdir/physpg-mask-${scanpgnum}-${physpgnum}.png info: || exit 1)
187 # ... crop and despeckle? the final pre-prepared image
188 convert $convertflags -crop $cropcords $physpg $cleanphyspg || exit 1
191 #detect if the page is 2-up
193 if [[ $logperphys == 2 ]] || ( [[ $logperphys != 1 ]] && $(dirname $0)/detect2pages.sh ${cleanphyspg} ${scanpgnum} ${physpgnum} ); then
194 if [[ $verbose == 1 ]]; then
195 echo "Resplitting physical page ${physpgnum}."
197 unpaper $unpaperflags --pre-rotate 90 --layout double --overwrite \
198 -op 2 --no-blackfilter --no-grayfilter --no-noisefilter \
199 --no-blurfilter $cleanphyspg $outdir/logpg-${scanpgnum}-${physpgnum}-%01d.pnm || exit 1
201 cp $cleanphyspg $outdir/logpg-${scanpgnum}-${physpgnum}-1.pnm
205 convert $convertflags $outdir/logpg-${scanpgnum}-${physpgnum}-?.pnm $outdir/logpg-${scanpgnum}-${physpgnum}-%01d.png || exit 1
207 #final convert and clean w/ bebook optimisation
208 if [[ $bebook ]]; then
209 convert $convertflags $outdir/logpg-${scanpgnum}-${physpgnum}-?.pnm -trim -fuzz 80% -resize 1200x1600 $outdir/final-${scanpgnum}-${physpgnum}-%01d.${extension} || exit 1
211 convert $convertflags $outdir/logpg-${scanpgnum}-${physpgnum}-?.pnm $outdir/final-${scanpgnum}-${physpgnum}-%01d.${extension} || exit 1
219 rm -rf $outdir/logpgs
220 ocropus book2pages $outdir/logpgs $outdir/logpg-*.png
221 ocropus pages2lines $outdir/logpgs
222 ocropus lines2fsts $outdir/logpgs
223 ocropus fsts2bestpaths $outdir/logpgs
224 ocropus buildhtml $outdir/logpgs > $outdir/out.html
226 mkdir -p $outdir/pages
227 mv $outdir/final-*.${extension} $outdir/pages