-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscanKodak.sh
executable file
·88 lines (76 loc) · 2.63 KB
/
scanKodak.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
# Scans from a hard-coded scanner to a pdf in the current directory
usage() {
echo "scanKodak.sh [OPTIONS...]"
echo
echo "General options:"
echo " -f FILE Give output file name [default: \`date +%F %H%M%S\`]"
echo " -c Scan in color [default: gray scale]"
echo " -q Set output JPEG quality to 85% [default: 50%]"
echo " -r RES Set resolution in dpi [default 150 dpi]"
echo " -# NUM Number of pages to scan"
echo
echo "Source selection:"
echo " -s Specify scan source: Normal | ADF Front | ADF Back | ADF Duplex"
echo " [default: ADF Duplex]"
echo " -o Scan only front pages from feeder (shortcut for -s 'ADF Front')"
echo " -d Scan front and back pages from feeder (shortcut for -s 'ADF Duplex')"
echo " [default]"
echo " -n Scan pages from flatbed (shortcut for -s 'Normal')"
}
gs='-type Grayscale'
qual='50%'
dpi='150'
correction="--contrast 70 --brightness -15"
paper_source='ADF Duplex'
page_count=''
# Get options
while getopts ":f:cqr:s:odn#:h" Option
do
case $Option in
f ) FILE=$OPTARG;;
c ) gs='';;
q ) qual='85%';;
r ) dpi=$OPTARG;;
s ) paper_source=$OPTARG;;
o ) paper_source='ADF Front';;
d ) paper_source='ADF Duplex';;
n ) paper_source='Normal'; page_count='--batch-count=1';;
'#' ) page_count="--batch-count=$OPTARG";;
h ) usage; exit;;
esac
done
TMPFILE=$(mktemp)
# Check if file is set
if [[ ! "$FILE" ]]
then
FILE="$(date +"%F %H%M%S").pdf"
fi
echo Will output to file "$FILE"
#Scan pages and store .tiffs
echo Starting scan...
scanimage --format tiff -p --batch=$TMPFILE%04d.tiff $page_count $correction --source "$paper_source" --resolution $dpi
if [ -z "`ls -1 $TMPFILE*.tiff`" ]; then
echo "Errors while scanning. Exiting without action."
exit 1
fi
#use imagemagick to compress all tiffs and glue them to a single pdf
echo Glueing pages...
convert $TMPFILE*.tiff $gs -quality $qual -density "$dpi"x"$dpi" -compress jpeg $TMPFILE.all.pdf
#Output to given file
if [[ ! -f $FILE ]]
then
#File does not exist. This is easy
echo Creating new PDF $FILE and fixing title.
#creating tmpfile because pdftk's update_info apparently can't read inline
echo -en "InfoKey: Title\nInfoValue: $FILE\n" > $TMPFILE.title
#updating the title because "tmp.rvvk8ozNjn.pdf" just doesn't look good in the title bar, also moving the file away from tmp
pdftk $TMPFILE.all.pdf update_info $TMPFILE.title output "$FILE"
else
#File exists. cat'ing files together
echo Appending to existing PDF $FILE
mv "$FILE" "$FILE.tmp.pdf"
pdftk "$FILE.tmp.pdf" $TMPFILE.all.pdf cat output "$FILE"
rm "$FILE.tmp.pdf"
fi
rm $TMPFILE*