-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathepson-scanner-automation-program.bash
executable file
·284 lines (231 loc) · 6.44 KB
/
epson-scanner-automation-program.bash
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/bash
# TYPE: Bash Shell script.
# PURPOSE: Scan documents on Epson scanner to PDF file. Can unite multiple PDF files in work directory, automatically send united PDF to your Email account, and then automatically archive them.
# REQUIRES: Ubuntu 16.04 LTS 64-bit or newer
# REQUIRES: ksh sane-utils tesseract-ocr tesseract-ocr-eng tesseract-ocr-nld tesseract-ocr-fra
# tesseract-ocr-deu msmtp heirloom-mailx imagemagick pdfunite
# REVISED: 2017/05/25
# AUTHOR: Mark Rijckenberg
# First execute these Terminal commands to install prerequisites:
# sudo apt update
# sudo apt install tesseract-ocr tesseract-ocr-eng tesseract-ocr-nld tesseract-ocr-fra tesseract-ocr-deu msmtp heirloom-mailx sane-utils
function cleanup {
rm -rf /tmp/*
}
function setdefaultvalues {
LANGUAGESETTING="eng"
RESOLUTIONSETTING="300"
EMAIL=""
COLORSETTING="Gray"
}
# 2017/5/25: scan2email function currently not working correctly...
function scan2email {
if [[ $pdfquantity -gt 1 ]];then
pdfunite *.pdf `echo $dateunitedpdf``echo $pdfname`
# First create and configure the files ~/.msmtprc and ~/.mailrc
# The file ~/.msmtprc should contain the line set sendmail=/usr/bin/msmtp
# Replace emailaddress@somewhere.com with the correct destination Email address
echo "Hi, here are my scanned files from `echo $dateunitedpdf`" | mail -s `echo $dateunitedpdf``echo $pdfname` -a `echo $dateunitedpdf``echo $pdfname` $EMAIL
mkdir `echo $dateunitedpdf`
mv *.pdf `echo $dateunitedpdf`
fi
if [[ $pdfquantity -eq 1 ]];then
# First create and configure the files ~/.msmtprc and ~/.mailrc
# The file ~/.msmtprc should contain the line set sendmail=/usr/bin/msmtp
# Replace emailaddress@somewhere.com with the correct destination Email address
mv *.pdf `echo $dateunitedpdf``echo $pdfname`
echo "Hi, here are my scanned files from `echo $dateunitedpdf`" | mail -s `echo $dateunitedpdf``echo $pdfname` -a `echo $dateunitedpdf``echo $pdfname` $EMAIL
mkdir `echo $dateunitedpdf`
mv *.pdf `echo $dateunitedpdf`
fi
}
function setlanguage {
echo "Press 1 to scan English document"
echo "Press 2 to scan Dutch document"
echo "Press 3 to scan French document"
echo "Press 4 to scan German document"
read LANG
if [ $LANG -eq 1 ];then
echo "English chosen"
LANGUAGESETTING="eng"
mainmenu
fi
if [ $LANG -eq 2 ];then
echo "Dutch chosen"
LANGUAGESETTING="nld"
mainmenu
fi
if [ $LANG -eq 3 ];then
echo "French chosen"
LANGUAGESETTING="fra"
mainmenu
fi
if [ $LANG -eq 4 ];then
echo "German chosen"
LANGUAGESETTING="deu"
mainmenu
fi
}
function setresolution {
echo "Press 1 to scan at 300 dpi"
echo "Press 2 to scan at 600 dpi"
echo "Press 3 to scan at 1200 dpi"
read RES
if [ $RES -eq 1 ]
then
echo "300 dpi chosen"
RESOLUTIONSETTING="300"
mainmenu
fi
if [ $RES -eq 2 ]
then
echo "600 dpi chosen"
RESOLUTIONSETTING="600"
mainmenu
fi
if [ $RES -eq 3 ]
then
echo "1200 dpi chosen"
RESOLUTIONSETTING="1200"
mainmenu
fi
}
function setemail {
echo "Which Email address should the scanned document(s) be sent to?"
read EMAIL
mainmenu
}
function setcolor {
echo "Press 1 to scan document in Gray only"
echo "Press 2 to scan document in Color"
read COLOR
if [ $COLOR -eq 1 ]
then
echo "Gray chosen"
COLORSETTING="Gray"
mainmenu
fi
if [ $COLOR -eq 2 ]
then
echo "Color chosen"
COLORSETTING="Color"
mainmenu
fi
}
function mainmenu {
WORKDIR=/tmp
tiffname=scannedfile.tiff
pdfname=scannedfile.pdf
datepdf=$(date +"%Y_%m_%d_%H_%M_%S")
dateunitedpdf=$(date +"%Y_%m_%d")
pdfquantity=$(ls $WORKDIR|grep pdf|wc -l)
cd $WORKDIR
echo "Language selected: $LANGUAGESETTING"
echo "Resolution selected: $RESOLUTIONSETTING"
echo "Email address selected: $EMAIL"
echo "Color selected: $COLORSETTING"
echo "Press 1 to scan single document and save it in your work directory as a PDF"
echo "Press 2 to unite multiple PDF files in your work directory, send united PDF to your Email account, and then archive them"
echo "Press 3 to exit shell program"
echo "Press 4 to set language"
echo "Press 5 to set resolution"
echo "Press 6 to set email"
echo "Press 7 to set color"
read ACTION
if [[ $ACTION -eq 1 ]]; then
SOURCE=""
if [ $# -gt 1 ]
then
SOURCE="--source ADF -l 3"
outname=temp
pbreak=$1
echo "$pbreak" | egrep "[^0-9,]+"
if [ $? -ne 1 ]
then
echo "Check Sequence List !"
exit 1
fi
else
pbreak=99
outname=$1
SOURCE="--batch-count=1"
fi
startdir=$(pwd)
tmpdir=scan-$RANDOM
cd /tmp
mkdir $tmpdir
cd $tmpdir
echo "################## Scanning ###################"
scanimage -x 210 -y 297 --batch=out%02d.tif --format=tiff --mode $COLORSETTING --resolution $RESOLUTIONSETTING $SOURCE
start=1
cnt=1
sc=$(echo "$pbreak" | cut -d"," -f1-99 --output-delimiter=" " | wc -w)
for pb in $(echo "$pbreak" | cut -d "," -f1-99 --output-delimiter=" "); do
ende=$(expr $start + $pb - 1)
pnr=0
i=1
echo "############ Page-Sequence ($cnt), Pages: $pb, Start: $start, End: $ende ############"
tpages=""
for page in $(ls out*.tif); do
pnr=$(expr $pnr + 1)
if [ $pnr -ge $start -a $pnr -le $ende ]
then
echo "... Converting"
# increase contrast and reduce colordepth
convert $page -level 15%,85% -depth 2 "b$page"
echo "... OCRing"
tpages="$tpages b$page"
i=$(expr $i + 1)
echo -n " "
tesseract $page $page -l $LANGUAGESETTING
if [ $sc -gt 1 ]
then
cnts=`printf %02d $cnt`
cat $page.txt >> $outname.$cnts.txt
else
cat $page.txt >> $outname.txt
fi
fi
done
echo "... Converting to PDF"
#Use tiffcp to combine output tiffs to a single multipage tiff
tiffcp $tpages output.tif
#Convert the tiff to PDF
if [ $sc -gt 1 ]
then
cnts=`printf %02d $cnt`
tiff2pdf -z output.tif > $startdir/$outname.$cnts.pdf
mv $outname.$cnts.txt $startdir
else
tiff2pdf -z output.tif > $startdir/$outname.pdf
mv $outname.txt $startdir
fi
start=$(expr $start + $pb)
cnt=$(expr $cnt + 1)
done
cd ..
echo "################ Cleaning Up ################"
rm -rf $tmpdir
cd $startdir
mv \.pdf `echo $datepdf``echo $pdfname`
rm \.txt
fi
if [[ $ACTION -eq 2 ]]; then
scan2email
fi
if [[ $ACTION -eq 4 ]]; then
setlanguage
fi
if [[ $ACTION -eq 5 ]]; then
setresolution
fi
if [[ $ACTION -eq 6 ]]; then
setemail
fi
if [[ $ACTION -eq 7 ]]; then
setcolor
fi
}
cleanup
setdefaultvalues
mainmenu