From 04b3ddfbf55d6ea7982c313f34e9ca8923d6d096 Mon Sep 17 00:00:00 2001 From: Daniel Axtens Date: Tue, 29 Mar 2011 21:55:09 +0800 Subject: [PATCH] Autodetect depth sensibly --- scan2pages.sh | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/scan2pages.sh b/scan2pages.sh index 995c5a6..c9a10c8 100755 --- a/scan2pages.sh +++ b/scan2pages.sh @@ -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; -- 2.20.1