Autodetect depth sensibly
authorDaniel Axtens <[email protected]>
Tue, 29 Mar 2011 13:55:09 +0000 (21:55 +0800)
committerDaniel Axtens <[email protected]>
Tue, 29 Mar 2011 13:55:09 +0000 (21:55 +0800)
scan2pages.sh

index 995c5a6..c9a10c8 100755 (executable)
@@ -7,7 +7,8 @@ Convert pdffile - a pdf of scanned facing pages - to a set of images in outdir,
 each with only one page.
 OPTIONS:
        -v: Be verbose.
-       -d depth: Use given depth. Default is 1. Unpaper can only handle up to 8.
+       -d depth: Use given depth. Default is 1 for black and white, 8 for colour.
+               Unpaper can only handle up to 8.
        -D dpi: Use given DPI. Colour scans on library scanners are usually 300, bw 400. 
        -s: skip masking/trimming. Overrides -m.
        -f: Forceably redo everything.
@@ -34,7 +35,8 @@ forceunpaper=
 forceclean=
 convertflags=
 unpaperflags=
-depthflags="-depth 1"
+depthset=
+depth=
 dpi=
 skipmask=
 verbose=
@@ -45,7 +47,7 @@ do
                convertflags="$convertflags -verbose"
                unpaperflags="$unpaperflags -v --time"
                ;;
-       d)      depthflags="-depth $OPTARG"
+       d)      depth="$OPTARG"
                ;;
        D)      dpi=$OPTARG
                ;;
@@ -111,6 +113,18 @@ if [[ "$dpi" = "" ]]; then
        fi
 fi
 
+# guess at depth, unless it's been set
+if [[ "$depth" = "" ]]; then
+       if [[ $( convert $file[0] -format "%k" info: || exit 1 ) == 2 ]]; then
+               depth=1;
+       else
+               depth=8
+       fi
+       if [[ $verbose == 1 ]]; then
+               echo "Depth guessed at $depth."
+       fi
+fi
+
 # process pages 1 by 1 to avoid convert gobbling all the memory
 for pg in `$my_seq 1 $pages`; do
        echo "Processing page $pg."
@@ -120,7 +134,7 @@ for pg in `$my_seq 1 $pages`; do
        # convert from pdf
        origpnm=$dir/pg-${pgn}.pnm
        if [[ ! $([ -e $origpnm ]) || $forcepdf ]]; then
-               convert $convertflags $depthflags -density $dpi $file[$(expr $pg - 1)] \
+               convert $convertflags -depth $depth -density $dpi $file[$(expr $pg - 1)] \
                        $origpnm || exit 1
        fi;
        

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