forked from burakbayramli/kod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.py
57 lines (50 loc) · 1.59 KB
/
scan.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
'''
Detect scanner type, start scanning from page 1, dumping results as
tiff and converting that tiff into jpg in reduced form. the detection
thing is needed because on my scanner plustek, everytime I plug it
into USB I get a different code for scanner id which I need to run the
scanning process
'''
import os, re, glob, sys, string;
def run_command(command):
'''
run shell command and return the output as list
'''
result = []
f = os.popen(command, "r")
sys.stdout.flush()
for l in f.xreadlines():
result.append(l)
return result
def detect():
'''
detect the scanner type, in format such as 001:003
'''
cmd = run_command("scanimage -L")
s = ""
for x in cmd: s += x
return re.search('(\d+:\d+)',s).group(1)
def two_digitize(i):
'''
turn single digit into two character digit
'''
if i < 10: return "0" + str(i)
return str(i)
i = 1 # by default start with page 1
if len(sys.argv) > 1: i = int(sys.argv[1])
color = "Gray"
if len(sys.argv) > 2: color = sys.argv[2]
# detect device
device = detect()
print i
print color
print device
while True:
print two_digitize(i)
# buyuk sayfa
#run_command("scanimage --mode=%s --resolution 200 -x 215 -y 297 -d plustek:libusb:%s --format=tiff > %s.tiff" % (color,device,two_digitize(i)))
# kucuk sayfa
run_command("scanimage --mode=%s --resolution 300 -x 215 -y 200 -d plustek:libusb:%s --format=tiff > %s.tiff" % (color,device,two_digitize(i)))
run_command("convert -scale %40 " + two_digitize(i) + ".tiff " + two_digitize(i) + ".jpg" )
i += 1
r = raw_input(">")