-
Notifications
You must be signed in to change notification settings - Fork 17
/
scan
executable file
·234 lines (197 loc) · 9.97 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# TODO
# - Do blur only one times
# - other layouts
# - fix content detection for romande energie
# - add http://www.graphicsmagick.org/GraphicsMagick.html#details-sharpen
import datetime
import glob
import os
import random
import re
import shutil
import subprocess
import time
import yaml
mogrify = ['gm', 'mogrify']
convert = ['gm', 'convert']
def call(cmd, cmd2=None, **kwargs):
print(' '.join(cmd) if isinstance(cmd, list) else cmd)
subprocess.check_call(cmd, **kwargs)
def output(cmd, cmd2=None, **kwargs):
print(' '.join(cmd) if isinstance(cmd, list) else cmd)
return subprocess.check_output(cmd, **kwargs)
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', '0x5', '-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, root_folder):
images = list()
if not os.path.exists(os.path.join(root_folder, 'process')):
os.makedirs(os.path.join(root_folder, 'process'))
if not os.path.exists(os.path.join(root_folder, 'scantailor')):
os.makedirs(os.path.join(root_folder, 'scantailor'))
if not os.path.exists(os.path.join(root_folder, 'unpaper')):
os.makedirs(os.path.join(root_folder, 'unpaper'))
if not os.path.exists(os.path.join(root_folder, 'deskew')):
os.makedirs(os.path.join(root_folder, 'deskew'))
for img in config['images']:
shutil.copyfile(os.path.join(root_folder, img), os.path.join(root_folder, 'process', os.path.basename(img)))
img = os.path.join(root_folder, 'process', 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, os.path.join(root_folder, 'scantailor')])
# img = os.path.join(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']:
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 0x5 -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, os.path.join(root_folder, 'scantailor')])
call(['gm', 'convert', os.path.join(root_folder, 'scantailor', '{}.tif'.format(name)), os.path.join(root_folder, 'scantailor', '{}.png'.format(name))])
img = os.path.join(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
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, '-noise', '7', '-blur', '0x5', '-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
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, os.path.join(root_folder, 'scantailor')
])
call([
'unpaper', img, os.path.join(root_folder, 'unpaper', os.path.basename(img))
])
call([
'/opt/deskew/Bin/deskew', '-o', os.path.join(root_folder, 'deskew', os.path.basename(img)), img
])
img2 = os.path.join(root_folder, os.path.basename(img))
subprocess.call(convert + [img, img2])
images.append(img2)
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', os.path.join(root_folder, 'final.png')])
images = [os.path.join(root_folder, 'final.png')]
return images
def finalise(config, root_folder):
full_name = config['full_name']
destination = config['destination']
if os.path.exists(destination):
return
images = config['images']
pdf = []
for img in images:
if os.path.exists(img):
name = os.path.splitext(os.path.basename(img))[0]
call(convert + [img, os.path.join(root_folder, '{}.pdf'.format(name))])
pdf.append(os.path.join(root_folder, '{}.pdf'.format(name)))
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())
try:
if config.get('waiting', False):
if not os.path.exists(os.path.join(root_folder, 'waiting')):
print("finalise: {}".format(os.path.basename(root_folder)))
finalise(config, root_folder)
else:
print("==== Still waiting: {} ====".format(os.path.basename(root_folder)))
else:
ok = True
for img in config['images']:
if not os.path.exists(os.path.join(root_folder, img)):
ok = False
if ok:
print("transform: {}".format(os.path.basename(root_folder)))
config['images'] = transform(config, root_folder)
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 = '/destination/{}.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
print("======")
print(os.path.basename(root_folder))
print("======")
except subprocess.CalledProcessError as e:
print(e)
with open(os.path.join(root_folder, 'error.yaml'), 'w') as error_file:
error_file.write(yaml.dump({'subproceesserror': e}))
# raise e
except Exception as e:
print(e)
with open(os.path.join(root_folder, 'error.yaml'), 'w') as error_file:
error_file.write(yaml.dump({'error': e}))
# raise e
time.sleep(30)