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)] \
129 cleanscanpg=$outdir/scanpg-clean-${scanpgnum}.pnm
130 if [ ! -e $cleanscanpg ]; then
132 # ... downscale, blur,
133 convert $convertflags -resize 10% -depth 8 -blur 10 -median 2 $scanpg $outdir/scanpg-mask-${scanpgnum}.png ||exit 1
135 # ... get crop co-ords. They're off by ~2 as I don't know how to
136 # properly correct for the border.
137 cropcords=$(convert -border 1x1 -bordercolor '#000' -resize 1000% -trim -fuzz 90% -format "%wx%h%O" $outdir/scanpg-mask-${scanpgnum}.png info: || exit 1)
139 # ... crop and despeckle? the final pre-prepared image
140 convert $convertflags -crop $cropcords $scanpg $cleanscanpg || exit 1
141 elif [[ $skipmask ]]; then
142 cp $origfile $preppnm
145 # check it hasn't mostly disappeared - e.g. if the scan was all black
146 # (e.g. forgot to put the book down when you first hit scan)
147 if [[ $(convert $cleanscanpg -format '%[fx:s.w*s.h>1000]' info:) = "0" ]]; then
148 [[ $verbose == 1 ]] && echo "Discarding pg ${scanpgnum}: not enough remains after masking."
153 physpgbase=$outdir/physpg-${scanpgnum}
154 if [ ! -e ${physpgbase}-1.pnm ] || [ ! -e ${physpgbase}-2.pnm ]; then
155 unpaper $unpaperflags --layout double --overwrite -ni 10 -op 2 $cleanscanpg ${physpgbase}-%01d.pnm || exit 1
158 for physpgnum in $($my_seq 1 2); do
159 echo Processing physical page ${physpgnum}.
161 physpg=${physpgbase}-${physpgnum}.pnm #$physpgbase has $outdir/ already
163 cleanphyspg=$outdir/physpg-clean-${scanpgnum}-${physpgnum}.pnm
165 if [ ! -e $cleanphyspg ]; then
166 # remask and retrim. FIXME: may throw off 2page detection
167 convert $convertflags -resize 10% -depth 8 -gamma 0.01 -median 2 $physpg $outdir/physpg-mask-${scanpgnum}-${physpgnum}.png ||exit 1
169 # Trim #-border 1x1 -bordercolor '#fff' -trim -fuzz 30%
170 cropcords=$(convert -trim -fuzz 90%\
171 -resize 1000% -format "%wx%h%O" $outdir/physpg-mask-${scanpgnum}-${physpgnum}.png info: || exit 1)
173 # ... crop and despeckle? the final pre-prepared image
174 convert $convertflags -crop $cropcords $physpg $cleanphyspg || exit 1
177 #detect if the page is 2-up
179 if [[ $logperphys == 2 ]] || ( [[ $logperphys != 1 ]] && $(dirname $0)/detect2pages.sh ${cleanphyspg} ${scanpgnum} ${physpgnum} ); then
180 if [[ $verbose == 1 ]]; then
181 echo "Resplitting physical page ${physpgnum}."
183 unpaper $unpaperflags --pre-rotate 90 --layout double --overwrite \
184 -op 2 --no-blackfilter --no-grayfilter --no-noisefilter \
185 --no-blurfilter $cleanphyspg $outdir/logpg-${scanpgnum}-${physpgnum}-%01d.pnm || exit 1
187 cp $cleanphyspg $outdir/logpg-${scanpgnum}-${physpgnum}-1.pnm
191 #final convert and clean w/ bebook optimisation
192 if [[ $bebook ]]; then
193 convert $convertflags -colorspace Gray -median 1 $outdir/logpg-${scanpgnum}-${physpgnum}-?.pnm -trim -fuzz 80% -resize 1200x1600 $outdir/final-${scanpgnum}-${physpgnum}-%01d.${extension} || exit 1
195 convert $convertflags $outdir/logpg-${scanpgnum}-${physpgnum}-?.pnm $outdir/final-${scanpgnum}-${physpgnum}-%01d.${extension} || exit 1
202 mkdir -p $outdir/pages
203 mv $outdir/final-*.${extension} $outdir/pages