forked from openpaperwork/pyinsane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan_adf.py
executable file
·57 lines (45 loc) · 1.5 KB
/
scan_adf.py
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
#!/usr/bin/env python
import os
import sys
import pyinsane.abstract as pyinsane
def main(args):
dstdir = args[0]
devices = pyinsane.get_devices()
assert(len(devices) > 0)
device = devices[0]
print("Will use the scanner [%s]" % (str(device)))
scanner_id = device.name
possible_srcs = device.options['source'].constraint
adf_src = None
for src in possible_srcs:
if "ADF" in src or "Feeder" in src:
adf_src = src
break
if adf_src is None:
print("No document feeder found")
sys.exit(1)
print("Will use the source [%s]" % adf_src)
device.options['source'].value = "ADF"
# Beware: Some scanner have "Lineart" or "Gray" as default mode
device.options['mode'].value = 'Color'
scan_session = device.scan(multiple=True)
try:
print("Scanning ...")
while True:
try:
scan_session.scan.read()
except EOFError:
print("Got page %d" % (len(scan_session.images)))
img = scan_session.images[-1]
imgpath = os.path.join(dstdir, "%d.jpg" %
(len(scan_session.images)))
img.save(imgpath)
except StopIteration:
print("Got %d pages" % len(scan_session.images))
if __name__ == "__main__":
args = sys.argv[1:]
if len(args) <= 0 or args[0][0] == '-':
print("Usage:")
print(" %s <dst directory>" % sys.argv[0])
sys.exit(1)
main(args)