-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.py
More file actions
executable file
·174 lines (142 loc) · 5.07 KB
/
Copy pathscanner.py
File metadata and controls
executable file
·174 lines (142 loc) · 5.07 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
#!/usr/bin/env python
tmpdir = '/var/local/share/tmp'
destdir = '/var/local/share'
logfile = '%s/scanlog.txt' % destdir
import argparse
import subprocess
from datetime import datetime
import logging
def config(folder, tmp_folder, log):
global logfile
global tmpdir
global destdir
logfile = log
tmpdir = tmp_folder
destdir = folder
logging.basicConfig(filename=logfile, level=logging.INFO)
def process_photo(infile, outfile):
cmd = ['gm']
cmd.append('convert')
cmd.extend(['-compress', 'jpeg'])
cmd.extend(['-quality', '80'])
cmd.append(infile)
cmd.append(outfile)
logging.info('Executing: %s' % subprocess.list2cmdline(cmd))
try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError, e:
logging.error('gm convert: %s' % e.output)
return ''
return destfilename
def is_blank_page(file):
cmd = ['gm']
cmd.append('identify')
cmd.append('-verbose')
cmd.append(file)
result = subprocess.check_output(cmd)
blank = True
import re
mStdDev = re.compile("""\s*Standard Deviation:\s*\d+\.\d+\s*\((?P<percent>\d+\.\d+)\).*""")
for line in result.splitlines():
match = mStdDev.search(line)
if match:
stdev = float(match.group('percent'))
#print stdev
if stdev > 0.1:
return False
break
return True
def create_pdf(inpattern, outfile, remove_blanks=True):
import glob
import os
process_cmd = ['gm']
process_cmd.append('convert')
process_cmd.extend(['-bordercolor','#9BA7AB'])
process_cmd.extend(['-border', '1x1'])
process_cmd.extend(['-fuzz', '18%'])
process_cmd.append('-trim')
# process_cmd.append('+adjoin')
listing = glob.glob(inpattern)
for filename in listing:
if not is_blank_page(filename):
cmd = list(process_cmd)
cmd.append(filename)
without_extension = os.path.splitext(filename)[0]
cmd.append('%s-trimmed.tiff' % without_extension)
logging.info('Executing: %s' % subprocess.list2cmdline(cmd))
try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError, e:
logging.error('gm convert: %s' % e.output)
return ''
else:
logging.info('%s is a blank page, skipping' % filename)
pattern_without_extension = os.path.splitext(inpattern)[0]
processed_pattern = '%s-trimmed.tiff' % pattern_without_extension
cmd = ['gm']
cmd.append('convert')
cmd.append('+dither')
cmd.extend(['-colors', '4'])
cmd.extend(['-blur', '1'])
cmd.extend(['-level', '10%,1,70%'])
cmd.append(processed_pattern)
cmd.append(outfile)
logging.info('Executing: %s' % subprocess.list2cmdline(cmd))
try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError, e:
logging.error('gm convert: %s' % e.output)
return ''
return True
def scan(mode):
global logfile
global tmpdir
global destdir
timestamp = datetime.now().strftime("%Y_%m_%d-%H_%M_%S")
tmpfilename_prefix = '%s/%s' % (tmpdir, timestamp)
destfileprefix = '%s/%s' % (destdir, timestamp)
logging.info('Scan attempt at %s ================' % timestamp)
if mode == 'photo':
destfilename = '%s.jpg' % destfileprefix
cmd = ['scanimage']
cmd.append('-B')
cmd.extend(['--mode', 'Color'])
cmd.extend(['--page-height', '0'])
cmd.extend(['--brightness', '30'])
cmd.extend(['--format', 'tiff'])
logging.info('Executing: %s' % subprocess.list2cmdline(cmd))
f = open('%s-scanned.tiff' % tmpfilename_prefix, 'w')
try:
subprocess.check_call(cmd, stdout=f)
except subprocess.CalledProcessError, e:
logging.error('scanimage: %s' % e.output)
return ''
if not process_photo(infile='%s-scanned.tiff' % tmpfilename_prefix,
outfile=destfilename):
return ''
return destfilename
elif mode in ['front', 'duplex']:
destfilename = '%s.pdf' % destfileprefix
scan_source = 'ADF Front' if mode == 'front' else 'ADF Duplex'
cmd = ['scanimage']
cmd.append('-B')
cmd.extend(['--mode', 'Color'])
cmd.extend(['--page-height', '0'])
cmd.extend(['--format', 'tiff'])
cmd.append('--batch=%s-scanned-%%02d.tiff' % tmpfilename_prefix)
cmd.extend(['--source', scan_source])
logging.info('Executing: %s' % subprocess.list2cmdline(cmd))
try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError, e:
# only 7 is ok, means feeder out of documents at the end of a scan
if e.returncode != 7:
logging.error('scanimage: %s' % e.output)
return ''
if not create_pdf(inpattern='%s-scanned-*.tiff' % tmpfilename_prefix,
outfile=destfilename):
return ''
return destfilename
else:
print 'not supported'
return ''