{ ^_^ } sinustrom Solving life, one problem at a time!

Batch scanning script

2007-11-03
Author: Zoltan Puskas
Categories: linux
Tags:

Though there are quite a few graphical applications for scanning under Linux, like XSane or Kooka, when doing batch scans of university notes, the extra clicking around makes the process of scanning pages one by one more painful. To speed up the process I wrote a simple script to make batch scanning easier.

Dependencies

The script assumes the following binaries to be installed on the system:

  • scanimage
  • convert

If you don’t have them already installed, just use your flavour of package manager to get them, e.g. under MEPIS Linux:

# apt-get install sane-utils imagemagick

Script

#!/bin/bash

#****************************************************************************#
# Bash scanning script
# Witten by Puskás Zoltán, 2007.11.02
# License: GNU/GPL2
# Version 2.0
#****************************************************************************#
echo -e "\nBatch scan script v2.0 by Puskás Zoltán, 2007.11.02\n"

#setup some global variables
RESOLUTION=150
SCANFORMAT="tiff"
EXTENSION="tiff"
CNT=1
X=210
Y=297
FILENAME="image"
QUALITY=75
KEEP=0

#parse parameters
while (( "$#" )); do
    case $1 in
        -jpg)
            EXTENSION="jpg"
            shift
        ;;
        -gif)
            EXTENSION="gif"
            shift
        ;;
        -png)
            EXTENSION="png"
            shift
        ;;
        -q)
            [ $2 ] && (( $2>0)) && QUALITY=$2
            [ $2 ] && shift
            shift
        ;;
        -c)
            [ $2 ] && (( $2>0)) && CNT=$2
            [ $2 ] && shift
            shift
        ;;
        -r)
            [ $2 ] && (( $2>0)) && RESOLUTION=$2
            [ $2 ] && shift
            shift
        ;;
        -f)
            [ $2 ] && FILENAME=$2
            [ $2 ] && shift
            shift
        ;;
        -a4)
            X=210
            Y=297
            shift
        ;;
        -a5)
            X=148
            Y=210
            shift
        ;;
        -a6)
            X=105
            Y=148
            shift
        ;;
        -b5)
            X=176
            Y=250
            shift
        ;;
        -ltr)
            X=215
            Y=279
            shift
        ;;
        -k)
            KEEP=1
            shift
        ;;
    esac
done

echo "Number of scans to be performed: $CNT"

#initlaize loop counter
i=0

#repeat actions
while [ $CNT != $i ]
do
    #display "contols"
    echo -e "\n>> To stop scanning press Ctrl+C"
    echo ">> To scan $FILENAME$i.$EXTENSION press Enter..."
    read

    #if file already exists then ask for confirmaiton
    if [ -e $FILENAME$i.$SCANFORMAT ]
    then
        echo "WARNING: $FILENAME$i.$SCANFORMAT already exists. Overwrite?"
        select ANSWER in "y" "n"
        do
            case $ANSWER in
                "y")
                    rm $FILENAME$i.$SCANFORMAT
                    break
                ;;
                "n")
                    echo "File not to be overwritten. Exiting"
                    exit 1
                ;;
            esac
        done
    fi

    #scan the image to raw
    echo ">> Executing command scanimage --format=$SCANFORMAT --resolution=$RESOLUTION -x $X -y $Y >> $FILENAME$i.$SCANFORMAT"
    scanimage --format=$SCANFORMAT --resolution=$RESOLUTION  -x $X -y $Y >> $FILENAME$i.$SCANFORMAT

    #if output format is different from raw, then convert
    if [ "$EXTENSION" != "$SCANFORMAT" ]
    then
        echo ">> Converting image from $FILENAME$i.$SCANFORMAT to $FILENAME$i.$EXTENSION"

        #if file already exists then ask for confirmaiton
        if [ -e $FILENAME$i.$EXTENSION ]
        then
            echo "WARNING: $FILENAME$i.$EXTENSION already exists. Overwrite?"
            select ANSWER in "y" "n"
            do
                case $ANSWER in
                    "y")
                        rm $FILENAME$i.$EXTENSION
                        break
                    ;;
                    "n")
                        echo "File not to be overwritten. Exiting"
                        exit 1
                    ;;
                esac
            done
        fi

        convert -quality $QUALITY $FILENAME$i.$SCANFORMAT $FILENAME$i.$EXTENSION
        if [ $KEEP -eq 0 ]
        then
            echo ">> Removing $FILENAME$i.$SCANFORMAT raw file"
            rm $FILENAME$i.$SCANFORMAT
        fi
    fi
    i=$[$i+1]
done

echo -e "\n...Done"
echo "All files were saved to the current directory!"

Example usage

To use it anywhere on the system place the script into /usr/local/bin and set the execution permissions on it.

The script supports setting final image format, typical paper sizes as well as resolution and quality parameters (see the beginning of the script for all the options and their default values). Example execution to scan three pieces of A4 sheets:

$ scan-batch -jpg -a4 -c 3

Finished items will be placed into the current working directory.


Content