-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscantoimage-0.2.4-1.sh
More file actions
executable file
·191 lines (161 loc) · 5.69 KB
/
scantoimage-0.2.4-1.sh
File metadata and controls
executable file
·191 lines (161 loc) · 5.69 KB
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
#! /bin/bash
set +o noclobber
#
# $1 = scanner device
# $2 = brother internal
#
# This is my scantoimage. It's mostly the same as Brothers script, but it allows some more environment variables to be set using the cfg file so we don't have to edit the script directly.
function Usage() {
echo -e "Usage:"
echo -e "\t "$0" [option] <devicename>\n"
echo -e "The devicename is optional. Set by default to ${default_device}"
echo -e "Check the source for options. Will write a $compress_format file if the compress variable is set to True. The current value is $compress. If set to False, then the scan format is $scan_format"
echo -e "Heights and width can be specified in the script. So can resolution."
echo -e "\nOptions:"
echo -e "\t -h \t Print this help"
}
# scan options
scan_format="pnm"
compress="True"
compress_format="jpg"
compress_quality="95"
autocrop="True"
#autocrop="False"
# set color to full color or 24 bit.
mode='"24Bit Color"' #"Black & White"'
scriptname=$(basename "$0")
# $0 refers to the script name
basedir=$(readlink -f "$0" | xargs dirname)
# change to directory of script
cd ${basedir}
echo "basedir = $basedir"
# ugly hack that makes environment variables set available
cfgfile=$(ls ../brscan-skey-*.cfg)
echo "cfgfile = $cfgfile"
if [[ -r "$cfgfile" ]]; then
echo "Found cfgfile"
source "$cfgfile"
echo "environment after processing cfgfile"
env
fi
# List devices with scanimage -L
# Query device with scanimage -h to get allowed resolutions
# In color, resolution more than 300 slows things down on lower model printers
if [[ -z "$IMAGEDPI" ]]; then
resolution=600
else
resolution="$IMAGEDPI"
fi
# height and width can be left empty to
# be autodetected
if [[ -n "$IMAGEHEIGHT" ]]; then
height="$IMAGEHEIGHT"
fi
if [[ -n "$IMAGEWIDTH" ]]; then
width="$IMAGEWIDTH"
fi
# SAVETO DIRECTORY
if [[ -z "$SAVETO" ]]; then
SAVETO=${HOME}'/brscan/images'
else
SAVETO=${SAVETO}'/images/'
fi
mkdir -p $SAVETO
if [[ -z $LOGDIR ]]; then
# if LOGDIR is not set, choose a default
mkdir -p ${HOME}/brscan
logfile=${HOME}"/brscan/$scriptname.log"
else
mkdir -p $LOGDIR
logfile=${LOGDIR}"/$scriptname.log"
fi
touch ${logfile}
# if SOURCE is not set
# this is currently unused in scantoimage
if [[ -z $SOURCE ]]; then
SOURCE="FlatBed" #"Automatic Document Feeder(left aligned)"
fi
# parse one simple option. Allows you to get help
while getopts "h" opt; do
case "$opt" in
h)
Usage
exit 0
# usually there will be shift here
shift
;;
esac
done
# see if scanners exists
default_device=$(scanimage -L | head -n 1 | sed "s/.*\`\(.*\)'.*/\1/")
if [[ -z "$default_device" ]]; then
echo "No devices found" | tee "$logfile"
fi
if [ -z "$1" ]; then
device="$default_device"
else
device=$1
fi
epochnow=$(date '+%s')
# for debugging purposes, output arguments
echo "options after processing." >> ${logfile}
echo "$*" >> ${logfile}
# export environment to logfile
set >> ${logfile}
echo $LOGDIR >> ${logfile}
# BEGIN SCAN PROCEDURE
if [ "`which usleep 2>/dev/null `" != '' ];then
usleep 100000
else
sleep 0.1
fi
output_file="$SAVETO"/brscan_image_"`date +%Y-%m-%d-%H-%M-%S`".pnm
# options
if [[ -z "$height" || -z "$width" ]]; then
SCANOPTIONS="--mode $mode --device-name \"$device\" --resolution $resolution --format $scan_format"
else
SCANOPTIONS="--mode $mode --device-name \"$device\" --resolution $resolution -x $width -y $height --format $scan_format"
fi
# echo the command to stdout. Then write it to logfile.
echo "scanimage $SCANOPTIONS > $output_file"
echo "scanimage $SCANOPTIONS > $output_file" >> $logfile
echo "scanimage $SCANOPTIONS > $output_file" 2>> $logfile | bash
#scanimage --verbose $SCANOPTIONS > $output_file 2>/dev/null
# if the file is zero size, run again.
if [ ! -s $output_file ];then
if [ "`which usleep 2>/dev/null `" != '' ];then
usleep 1000000
else
sleep 1
fi
echo "Rerunning scanimage $SCANOPTIONS"
scanimage $SCANOPTIONS > $output_file 2>/dev/null
fi
if [ -s $output_file ]; then
echo $output_file is created. | tee -a "$logfile"
output_file_cropped=$(dirname $output_file)"/"$(basename $output_file .pnm)"-cropped.pnm"
if [[ "True" == "$autocrop" ]]; then
# maybe better to use autocrop script, which seems better for trimming dirty scanned borders
#echo convert -trim -fuzz 10% -bordercolor white -border 20x10 +repage "$resolution" $output_file "$output_file_cropped" | bash
# get some autotrimming information about the image
image_info=$(convert $output_file -virtual-pixel edge -scale 10% -blur 0x20 -resize 1000% -fuzz 5% -trim info:)
# compute an offset
off=$(echo $image_info | awk '{print $4 }' | sed -e 's/[^+]*\(+[0-9]*+[0-9]*\)/\1/')
# calculate crop
crop=$(echo $image_info | awk '{print $3}')
echo "convert $output_file -crop $crop$off $output_file_cropped" | tee -a "$logfile"
# run convert command
if convert $output_file -crop $crop$off "$output_file_cropped"; then
# if the convert command converts successfully
#output_file="$output_file_cropped"
cp "$output_file_cropped" "$output_file"
rm "$output_file_cropped"
fi
fi
# Should convert to jpg and delete duplicates
output_file_compressed=$(dirname $output_file)"/"$(basename $output_file .pnm)".$compress_format"
if [[ "True" == "$compress" ]]; then
echo convert -quality $compress_quality -density "$resolution" $output_file "$output_file_compressed" | tee -a $logfile | bash
# file ownership is best set through default acl for the destination directory
fi
fi