Whatever it was I was doing when semester got busy.
[dja/scandal.git] / scan2pages.sh
index 6d232f5..ccb464b 100755 (executable)
@@ -132,7 +132,7 @@ for pg in `$my_seq 1 $pages`; do
        
        # convert from pdf
        origfile=$dir/pg-${pgn}.png
-       if [ ! -e $origpnm ] || [[ $forcepdf ]]; then
+       if [ ! -e $origfile ] || [[ $forcepdf ]]; then
                convert $convertflags -depth $depth -density $dpi $file[$(expr $pg - 1)] \
                        $origfile || exit 1
        fi;
@@ -149,9 +149,9 @@ for pg in `$my_seq 1 $pages`; do
                cropcords=$(convert -border 1x1 -bordercolor '#000' -resize 1000% -trim -fuzz 90% -format "%wx%h%O" $dir/pg-mask-${pgn}.png info: || exit 1)
                
                # ... crop and despeckle? the final pre-prepared image
-               convert $convertflags -crop $cropcords $origpnm $preppnm || exit 1
+               convert $convertflags -crop $cropcords $origfile $preppnm || exit 1
        elif [[ $skipmask ]]; then
-               cp $origpnm $preppnm
+               cp $origfile $preppnm
        fi;
        
        # check it hasn't mostly disappeared - e.g. if the scan was all black
@@ -168,29 +168,44 @@ for pg in `$my_seq 1 $pages`; do
                unpaper $unpaperflags --layout double --overwrite -ni 10 -op 2 $preppnm ${unppnmbase}-%01d.pnm || exit 1
        fi;
        
-       #detect if the page is 2-up
        for subpg in $($my_seq 1 2); do
                echo Processing subpg ${subpg}.
-               if $(dirname $0)/detect2pages.sh $dir/upg-${pgn}-${subpg}.pnm ${pgn} ${subpg}; then
+               
+               subpgfile=${unppnmbase}-${subpg}.pnm #$unppnmbase has $dir/ already
+               
+               prepsubpnm=$dir/upg-pp-${pgn}-${subpg}.pnm
+               
+               # remask and retrim. FIXME: may throw off 2page detection
+               convert $convertflags -resize 10% -depth 8 -gamma 0.01 -median 2 $subpgfile $dir/spg-mask-${pgn}-${subpg}.png ||exit 1
+
+               # Trim #-border 1x1 -bordercolor '#fff' -trim -fuzz 30% 
+               cropcords=$(convert -trim -fuzz 90%\
+                -resize 1000% -format "%wx%h%O" $dir/spg-mask-${pgn}-${subpg}.png info: || exit 1)
+               
+               # ... crop and despeckle? the final pre-prepared image
+               convert $convertflags -crop $cropcords $subpgfile $prepsubpnm || exit 1
+               
+               #detect if the page is 2-up
+
+               if $(dirname $0)/detect2pages.sh ${prepsubpnm} ${pgn} ${subpg}; then
                        if [[ $verbose == 1 ]]; then
                                echo "Resplitting subpg ${subpg}."
                        fi
-                       unpaper $unpaperflags --pre-rotate 90 --layout double --overwrite -op 2 $dir/upg-${pgn}-${subpg}.pnm $dir/upg-${pgn}-${subpg}-%01d.pnm
+                       unpaper $unpaperflags --pre-rotate 90 --layout double --overwrite -op 2 $prepsubpnm $dir/upg-${pgn}-${subpg}-%01d.pnm || exit 1
                else
-                       cp $dir/upg-${pgn}-${subpg}.pnm $dir/upg-${pgn}-${subpg}-1.pnm
+                       cp $prepsubpnm $dir/upg-${pgn}-${subpg}-1.pnm
                fi;
 
+       
+               #final convert and clean w/ bebook optimisation
+               if [[ $bebook ]]; then
+                       convert $convertflags -colorspace Gray -median 1 $dir/upg-${pgn}-${subpg}-?.pnm -trim -fuzz 80% -resize 1200x1600 $dir/final-${pgn}-${subpg}-%01d.${extension} || exit 1
+               else
+                       convert $convertflags $dir/upg-${pgn}-${subpg}-?.pnm $dir/final-${pgn}-${subpg}-%01d.${extension} || exit 1
+               fi
+       
        done;
 
-       #final convert and clean w/ bebook optimisation
-       if [[ $bebook ]]; then
-               convert $convertflags -colorspace Gray -median 1 $dir/upg-${pgn}-1-?.pnm -trim -resize 1800x2400 $dir/final-${pgn}-1-%01d.${extension} || exit 1
-               convert $convertflags -colorspace Gray -median 1 $dir/upg-${pgn}-2-?.pnm -trim -resize 1800x2400 $dir/final-${pgn}-2-%01d.${extension} || exit 1
-       else
-               convert $convertflags $dir/upg-${pgn}-1-?.pnm $dir/final-${pgn}-1-%01d.${extension} || exit 1
-               convert $convertflags $dir/upg-${pgn}-2-?.pnm $dir/final-${pgn}-2-%01d.${extension} || exit 1
-       fi
-       
 done
 
 mkdir -p $dir/pages

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