-
Notifications
You must be signed in to change notification settings - Fork 17
/
postprocess
executable file
·244 lines (200 loc) · 9.15 KB
/
postprocess
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
236
237
238
239
240
241
242
243
244
#!/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 traceback
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_output(cmd, stderr=subprocess.PIPE, **kwargs)
def output(cmd, cmd2=None, **kwargs):
print(' '.join(cmd) if isinstance(cmd, list) else cmd)
return subprocess.check_output(cmd, stderr=subprocess.PIPE, **kwargs)
def crop(img, marging_horizontal=25, maring_vertical=25, rm_top=25, rm_left=15):
"""
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 - rm_left, o_h - rm_top, rm_left, rm_top),
'+repage', '-virtual-pixel', 'edge', '-noise', '7', '-blur', '0x5', '-fuzz', '25%', '-format', '%@', 'info:-'
]).strip())]
if h <= o_h and w <= o_w:
call(convert + [img, '-background', '#ffffff', '-crop', '{}x{}+{}+{}'.format(
w + 2 * marging_horizontal, h + 2 * maring_vertical, x - marging_horizontal + rm_left, y - maring_vertical + rm_top
), '+repage', img])
def save(root_folder, img, folder):
if os.environ.get("PROGRESS") == "TRUE":
if not os.path.exists(os.path.join(root_folder, folder)):
os.makedirs(os.path.join(root_folder, folder))
shutil.copyfile(img, os.path.join(root_folder, folder, os.path.basename(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, 'scantailor1200')):
# os.makedirs(os.path.join(root_folder, 'scantailor1200'))
# 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))
# force-cleanup
if config['args']['force_cleanup']:
call(mogrify + ['-level', '15%,1,85%', img])
else:
call(mogrify + ['-level', '10%,1,90%', img])
save(root_folder, img, '1-cleanup')
marging_horizontal=100
maring_vertical=100
crop(img, round(marging_horizontal/10/2.51*300), round(maring_vertical/10/2.51*300))
save(root_folder, img, '2-precrop')
call(['/opt/Deskew/Bin/deskew', '-b', 'FFFFFF', '-o', img, img])
save(root_folder, img, '3-deskew')
# Margin in mm
marging_horizontal=10
maring_vertical=7
crop(img, round(marging_horizontal/10/2.51*300), round(maring_vertical/10/2.51*300))
save(root_folder, img, '4-crop')
# sharpen
call(mogrify + ['-sharpen', '0x2', img])
save(root_folder, img, '5-sharpen')
# dither
#call(mogrify + ['+dither', img])
# auto-rotate
try:
orientation = 0
orientation = output(['tesseract', img, '-', '--psm', '0', '-l', 'osd']).decode('utf-8')
# orientation = 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])
save(root_folder, img, '6-autorotate')
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',
# '--output-dpi=300', '--color-mode=color_grayscale',
# img, os.path.join(root_folder, 'scantailor')
# ])
# call([
# 'scantailor-cli', '--dpi=300', '--output-dpi=1200',
# img, os.path.join(root_folder, 'scantailor1200')
# ])
# 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))
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['transformed_images']
pdf = []
for img in images:
if os.path.exists(img):
name = os.path.splitext(os.path.basename(img))[0]
#call('tesseract -l fra+eng {} stdout pdf > {}'.format(
# img, os.path.join(root_folder, '{}.pdf'.format(name))
#), shell=True)
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)
# call(['chmod', '-R', 'go+rw', '/destination'])
print('Welcome to scaned images document to paperless.')
while True:
print("Scan")
for config_file_name in glob.glob('/source/*/config.yaml'):
if not os.path.exists(config_file_name):
continue
print(re.sub(r'.', '-', config_file_name))
print(config_file_name)
root_folder = os.path.dirname(config_file_name)
if os.path.exists(os.path.join(root_folder, 'error.yaml')):
print("On error")
continue
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")
finalise(config, root_folder)
else:
print("Still waiting")
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")
config['transformed_images'] = transform(config, root_folder)
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:
print(e)
with open(os.path.join(root_folder, 'error.yaml'), 'w') as error_file:
error_file.write(yaml.dump({
'subproceesserror': e,
'traceback': traceback.format_exc().split('\n'),
}))
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,
'traceback': traceback.format_exc().split('\n'),
}))
# try:
# if os.path.exists(root_folder):
# call(['chmod', '-R', 'go+rw', root_folder])
# 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}))
print("Sleep")
time.sleep(30)