-
Notifications
You must be signed in to change notification settings - Fork 17
/
scan
235 lines (202 loc) · 9.74 KB
/
scan
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/home/sbrunner/.py/bin/python3
# -*- coding: utf-8 -*-
# TODO
# - Do blure only one times
# - other layouts
# - fix content detection for romande energie
# - add http://www.graphicsmagick.org/GraphicsMagick.html#details-sharpen
import subprocess
import os
import shutil
import datetime
import random
import re
import glob
mogrify = ['gm', 'mogrify']
convert = ['gm', 'convert']
"""def call(cmd, cmd2=None, **kwargs):
print(' '.join(cmd) if isinstance(cmd, list) else cmd)
success = False
while not success:
try:
subprocess.check_call(cmd, **kwargs)
success = True
except subprocess.CalledProcessError:
if cmd2 is not None:
subprocess.call(cmd2)
pass
def output(cmd, cmd2=None, **kwargs):
print(' '.join(cmd) if isinstance(cmd, list) else cmd)
success = False
while not success:
try:
subprocess.check_output(cmd, **kwargs)
success = True
except subprocess.CalledProcessError:
if cmd2 is not None:
subprocess.call(cmd2)
pass
"""
def call(cmd, cmd2=None, **kwargs):
print(' '.join(cmd) if isinstance(cmd, list) else cmd)
try:
subprocess.check_call(cmd, **kwargs)
except subprocess.CalledProcessError as e:
print(e)
exit(1)
def output(cmd, cmd2=None, **kwargs):
print(' '.join(cmd) if isinstance(cmd, list) else cmd)
try:
return subprocess.check_output(cmd, **kwargs)
except subprocess.CalledProcessError as e:
print(e)
exit(1)
def old_crop(img, marging_horizontal=25, maring_vertical=25):
"""
Margin in px
"""
o_w, o_h = [int(e) for e in output(convert + [img, '-format', '%w %h', 'info:-']).strip().split(b' ')]
w, h, x, y = [int(e) for e in re.split(b'[x+]', output(convert + [
img, '-crop', '{}x{}+{}+{}'.format(o_w - 2 * marging_horizontal, o_h - 2 * maring_vertical, marging_horizontal, maring_vertical),
'+repage', '-virtual-pixel', 'edge', '-noise', '7', '-blur', '0x3', '-fuzz', '25%', '-format', '%@', 'info:-'
]).strip())]
if h <= o_h and w <= o_w:
call(convert + [img, '-crop', '{}x{}+{}+{}'.format(w + 2 * marging_horizontal, h + 2 * maring_vertical, x, y), '+repage', img])
def transform(config):
images = set()
os.makedirs(root_folder + '/scantailor')
os.makedirs(root_folder + '/preview')
for img in config['images']:
shutil.copyfile(os.path.join(root_folder, img), os.path.join(root_folder, os.path.basename(img)))
img = os.path.join(root_folder, os.path.basename(img))
# call(mogrify + ['-level-colors', '#202020,#d0d0d0', img])
# force-cleanup
if config['args']['force_cleanup']:
call(mogrify + ['-level', '15%,1,85%', img])
else:
call(mogrify + ['-level', '10%,1,90%', img])
# crop
# if config['args']['no_crop'] or config['args']['old_crop']:
# call(['scantailor-cli', '--layout=1', '--dpi=300', '--output-dpi=300', '--color-mode=color_grayscale', img, root_folder + '/scantailor'])
# img = root_folder + '/scantailor/{}.tif'.format(name)
# Margin in mm
marging_horizontal=10
maring_vertical=5
if not config['args']['no_crop']:
if not config['args']['new_crop']:
call([
'scantailor-cli', '--dpi=300', '--content-detection=normal',
'--content-box=0x0:1000x1000',
'--output-dpi=300', '--color-mode=color_grayscale', '--white-margins=true',
'--margins-left=0', '--margins-right=0', '--margins-top=0', '--margins-bottom=0',
img, root_folder + '/scantailor'])
old_crop(img, round(marging_horizontal/10/2.51*300), round(maring_vertical/10/2.51*300))
"""
call(
"gm mogrify {img} -crop `gm convert {img} -crop \\`gm convert {img} -format '%[fx:w-50]x%[fx:h-50]' "
"info:\\`+25+25 +repage -virtual-pixel edge -noise 7 -blur 0x4 -level 20%,100%,4 -fuzz 15% "
"-trim -format '%[fx:w+50]x%[fx:h+50]+%[fx:page.x]+%[fx:page.y]' info:` +repage {img}".format(img=img),
shell=True,
)"""
else:
call([
'scantailor-cli', '--layout=1.5', '--dpi=300', '--content-detection=cautious',
'--output-dpi=300', '--color-mode=color_grayscale', '--white-margins=true',
'--margins-left={}'.format(marging_horizontal), '--margins-right={}'.format(marging_horizontal),
'--margins-top={}'.format(maring_vertical), '--margins-bottom={}'.format(maring_vertical),
img, root_folder + '/scantailor'])
call(['gm', 'convert', root_folder + '/scantailor/{}.tif'.format(name), root_folder + '/scantailor/{}.png'.format(name)])
img = root_folder + '/scantailor/{}.png'.format(name)
"""
try:
subprocess.check_call(['identify', '-verbose', img])
except subprocess.CalledProcessError:
print("Ignore empty image: {}".format(img))
continue"""
# sharpen
call(mogrify + ['-sharpen', '0x2', img], ['identify', '-verbose', img])
# dither
#call(mogrify + ['+dither', img])
# auto-rotate
# if not config['args']['adf']:
try:
orientation = 0
orientation = subprocess.check_output(['tesseract', img, '-', '--psm', '0', '-l', 'osd']).decode('utf-8')
# orientation = subprocess.check_output(['tesseract', img, '-', '--psm', '0', '-l', 'fra+eng']).decode('utf-8')
orientation = [e for e in orientation.splitlines() if 'Orientation in degrees' in e]
orientation = '-' + orientation[0].split()[3]
call(mogrify + ['-rotate', orientation, img])
except subprocess.CalledProcessError as e:
print(e)
# is empty ?
w, h = [int(e) for e in output(convert + [img, '-format', '%w %h', 'info:-']).split(b' ')]
if output(
# convert + [img, '+repage', '-virtual-pixel', 'edge', '-noise', '7', '-blur', '0x4', '-level', '20%,1,80%', '-fuzz', '15%', '-trim', '-format', '%wx%h', 'info:-']) == b'1x1':
convert + [img, '-noise', '7', '-blur', '0x3', '-level', '20%,1,80%', '-draw', 'point {},{}'.format(w/2, h/2), '-format', '%@', 'info:-'])[0:4] == b'1x1+':
print("Ignore image with no content: {}".format(img))
continue
img2 = root_folder + '/preview/{}.png'.format(name)
subprocess.call(convert + [img, img2])
images.add(img2)
def finalise(config):
images = sorted(images)
pdf = []
if config['args']['append_credit_card']:
images2 = []
for img in images:
if os.path.exists(img):
images2.append(img)
# - append (credit cards) - convert clp.png clp.png -background color? -append page.png
call(['convert'] + images2 + ['-background', '#ffffff', '-append', root_folder + '/final.png'])
images = [root_folder + '/final.png']
for img in images:
if os.path.exists(img):
name = os.path.splitext(os.path.basename(img))[0]
call(convert + [img, root_folder + '/{}.pdf'.format(name)])
pdf.append(root_folder + '/{}.pdf'.format(name))
full_name = config['full_name']
destination = config['destination']
call(['pdftk'] + pdf + ['output', destination, 'compress'])
call(['exiftool', '-overwrite_original_in_place', '-Title=' + full_name, destination])
shutil.rmtree(root_folder)
while True:
for config_file_name in glob.glob('/source/*/config.yaml'):
root_folder = os.path.dirname(config_file_name)
with open(config_file_name) as config_file:
config = yaml.load(config_file.read())
args = parser.parse_args(config['args'])
try:
if config.get('waiting', False):
if not os.exist(os.path.join(root_folder, 'waiting')):
finalise(config)
else:
ok = True
for img in config['images']:
if not os.exists(os.path.join(root_folder, img)):
ok = False
if ok:
transform(config)
full_name = ' '.join(config['args']['title'])
if config['args']['correspondent'] is not None:
full_name = '{} - {}'.format(config['args']['correspondent'], full_name)
if config['args']['date'] is not None:
full_name = '{}Z - {}'.format(config['args']['date'], full_name)
if len(config['args']['tags']) > 0:
full_name = '{} - {}'.format(full_name, ','.join(config['args']['tags']))
destination = '/home/sbrunner/dsl/paperless/consume/{}.pdf'.format(
full_name
)
config['full_name'] = full_name
config['destination'] = destination
config['waiting'] = True
with open(config_file_name, 'w') as config_file:
config_file.write(yaml.dump(config))
with open( os.path.join(root_folder, 'waiting'), 'w') as waiting_file:
pass
except subprocess.CalledProcessError as e:
with open(os.path.join(root_folder, 'error.yaml'), 'w') as error_file:
errorfile.write(yaml.dump({'subproceesserror': e}))
except Error as e:
with open(os.path.join(root_folder, 'error.yaml'), 'w') as error_file:
errorfile.write(yaml.dump({'error': e}))
time.sleep(10)