-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan2image.sh
More file actions
executable file
·83 lines (76 loc) · 2.67 KB
/
Copy pathscan2image.sh
File metadata and controls
executable file
·83 lines (76 loc) · 2.67 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
#!/bin/bash
# scan2image.sh: Scan Key Daemon scan-to-image script example
# Copyright (C) 2016 Frank Abelbeck <frank.abelbeck@googlemail.com>
#
# This file is part of the brscankeyd program "brscankeyd.py".
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>."""
#
# usage: scan2image.sh devicename [resolution]
#
if [ $# -eq 0 ]; then
cat <<ENDOFMSG
Need at least one argument (SANE device address)!
usage: scan2image.sh SANEADDRESS [RESOLUTION]
RESOLUTON defaults to 600 (dpi).
ENDOFMSG
exit 1
fi
if [ ! -e "/usr/bin/scanimage" ]; then
echo "Could not find scanimage (part of sane-backends)!"
exit 1
fi
if [ ! -e "/usr/bin/convert" ]; then
echo "Could not find convert (part of imagemagick)!"
exit 1
fi
#
# create unique filename from date and if necessary from an index
#
# target directory for scans (created automatically if it does not exist):
# - if SCANDIR is set in the environment (e.g. from "scan dir" in the .ini, or
# exported manually), that wins;
# - otherwise default to a "Scans" folder inside the user's XDG Pictures dir
# (localised, e.g. ~/Afbeeldingen on a Dutch system), falling back to
# ~/Pictures if the xdg-user-dir helper is unavailable.
if [ -z "$SCANDIR" ]; then
if command -v xdg-user-dir >/dev/null 2>&1; then
SCANDIR="$(xdg-user-dir PICTURES)/Scans"
else
SCANDIR="$HOME/Pictures/Scans"
fi
fi
/bin/mkdir -p "$SCANDIR"
DATE="$(/bin/date +%Y-%m-%d)"
FILENAME="$SCANDIR/"$DATE"_scan.jpg"
N=-1
while [ -e "$FILENAME" ]; do
# filename already exists: increment counter, try again
let "N++"
FILENAME="$SCANDIR/"$DATE"_scan_$N.jpg"
done
if [ -n "$2" ]; then
RESOLUTION="$2"
else
RESOLUTION=300
fi
# perform the scan
# (1) scan the flatbed to a temporary image (scanimage emits PNM on stdout;
# status messages go to stderr and are captured in the daemon's log)
# (2) convert that image to a JPG at the target location
# (3) remove the temporary image
TMPIMG="$(/usr/bin/mktemp -u).pnm"
/usr/bin/scanimage --device-name="$1" --mode="24bit Color[Fast]" --resolution="$RESOLUTION" --source="FlatBed" >"$TMPIMG"
/usr/bin/convert "$TMPIMG" -quality 92 "$FILENAME"
/bin/rm -f "$TMPIMG"