-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.py
435 lines (372 loc) · 17.6 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/python3
# Creator: Thiemo Schuff, [email protected]
# Source: https://github.com/Starwhooper/RPi-docscan
#######################################################
#
# Prepare
#
#######################################################
# -*- coding:utf-8 -*-
from PIL import Image,ImageDraw,ImageFont,ImageColor
from PyPDF2 import PdfFileMerger
from datetime import datetime
import RPi.GPIO as GPIO
import json
import os
import requests
import subprocess
import sys
import time
import urllib.request
##### import config.json
try:
with open(os.path.split(os.path.abspath(__file__))[0] + '/config.json','r') as file:
cf = json.loads(file.read())
except:
sys.exit('exit: The configuration file ' + os.path.split(os.path.abspath(__file__))[0] + '/config.json does not exist or has incorrect content. Please rename the file config.json.example to config.json and change the content as required ')
#######################################################
#
# Init Screen
#
#######################################################
sys.path.append(os.path.split(os.path.abspath(__file__))[0] + '/waveshare144')
import LCD_1in44
import LCD_Config
KEY_UP_PIN = 6
KEY_DOWN_PIN = 19
KEY_LEFT_PIN = 5
KEY_RIGHT_PIN = 26
KEY_PRESS_PIN = 13
KEY1_PIN = 21
KEY2_PIN = 20
KEY3_PIN = 16
#######################################################
#
# get system informiation only one time at start
#
#######################################################
#init GPIO
GPIO.setmode(GPIO.BCM)
#GPIO.cleanup()
GPIO.setup(KEY_UP_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(KEY_DOWN_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(KEY_LEFT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(KEY_RIGHT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(KEY_PRESS_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(KEY1_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(KEY2_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
GPIO.setup(KEY3_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up
# 240x240 display with hardware SPI:
disp = LCD_1in44.LCD()
Lcd_ScanDir = LCD_1in44.SCAN_DIR_DFT #SCAN_DIR_DFT = D2U_L2R
disp.LCD_Init(Lcd_ScanDir)
disp.LCD_Clear()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = 128
height = 128
image = Image.new('RGB', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
#draw.rectangle((0,0,width,height), outline=0, fill=0)
#disp.LCD_ShowImage(image,0,0)
#######################################################
#
# functions
#
#######################################################
def scantime():
return(datetime.now().strftime(cf['filename']['timestamp']))
def ocrspace(documentfile):
# cf_ocrspace_apikey = 'K85906370488957'
payload = {'isOverlayRequired': False, 'apikey': cf['ocrspace']['apikey'], 'isCreateSearchablePdf' : True, 'isSearchablePdfHideTextLayer' : True, 'OCREngine' : 2, }
with open(documentfile, 'rb') as f:
r = requests.post('https://api.ocr.space/parse/image', files={documentfile: f}, data=payload, )
pdfcontent = json.loads(r.content.decode())
urllib.request.urlretrieve(pdfcontent['SearchablePDFURL'], documentfile[0:-4] + '.pdf')
def pdfmerge(cf,jobtime,documente_jpg):
merger = PdfFileMerger()
for file in document_jpg:
pdffile = file[0:-4] + '.pdf'
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 0), 'merge', fill = cf['color']['font'])
draw.text((0, 10), os.path.basename(pdffile), fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
print('add ' + pdffile)
merger.append(pdffile)
os.remove(pdffile)
print('merge all to ' + cf['folder']['destination'] + '/' + cf['filename']['prefix'] + jobtime + ".pdf")
merger.write(cf['folder']['destination'] + '/' + cf['filename']['prefix'] + jobtime + ".pdf")
merger.close()
def send_to_pushover(source,jobtime,format,cf):
thumbnailfile = cf['folder']['destination'] + "/thumb." + cf["filename"]["prefix"] + jobtime + ".jpg"
os.system("convert -thumbnail 200 " + source + " " + thumbnailfile)
r = requests.post("https://api.pushover.net/1/messages.json", data = {
"token": cf['pushover']['apikey'],
"user": cf['pushover']['userkey'],
# "html": 1,
# "priority": 1,
"message": 'image scan_' + jobtime + '.' + format + ' scanned'
}
,
files = {
"attachment": ("thumb.jpg", open(thumbnailfile, 'rb'), "image/jpg")
}
)
os.remove(thumbnailfile)
return()
#######################################################
#
# do magic
#
#######################################################
while 1:
scannerfound = 0
while scannerfound == 0:
try:
scanner = subprocess.check_output("/usr/bin/scanimage -n --device-name='" + cf['devicename'] + "' --format=jpeg", shell=True)
scannerfound = 1
except:
scannerfound = 0
if scannerfound == 0:
draw.rectangle((0,0,width,height), outline=0, fill="RED")
draw.text((10, 10), 'wait for scanner', fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
time.sleep(2)
destinationfound = 0
while destinationfound == 0:
if os.path.exists(cf['folder']['destination']) == True:
destinationfound = 1
else:
draw.rectangle((0,0,width,height), outline=0, fill="RED")
draw.text((5, 10), 'destination not', fill = cf['color']['font'])
draw.text((5, 20), 'available', fill = cf['color']['font'])
draw.text((5, 40), cf['folder']['destination'][0:19], fill = cf['color']['font'])
draw.text((5, 50), cf['folder']['destination'][20:39], fill = cf['color']['font'])
draw.text((5, 60), cf['folder']['destination'][40:59], fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
time.sleep(2)
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 10), 'ready', fill = cf['color']['font'])
draw.text((10, 30), 'Image File ->', fill = cf['color']['font'])
draw.text((10, 60), 'Scan Photo ->', fill = cf['color']['font'])
draw.text((10, 90), 'PDF Document ->', fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
time.sleep(0.2)
#scan Images
if GPIO.input(KEY1_PIN) == 0: # button is released
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 10), 'which file formant ?', fill = cf['color']['font'])
draw.text((10, 30), 'PNG ->', fill = cf['color']['font'])
draw.text((10, 60), 'JPEG ->', fill = cf['color']['font'])
draw.text((10, 90), 'TIFF ->', fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
time.sleep(1)
fileformat = 'xxx'
fileextension = 'xxx'
while fileformat == 'xxx':
if GPIO.input(KEY1_PIN) == 0:
fileformat = 'png'
fileextension = 'png'
elif GPIO.input(KEY2_PIN) == 0:
fileformat = 'jpeg'
fileextension = 'jpg'
elif GPIO.input(KEY3_PIN) == 0:
fileformat = 'tiff'
fileextension = 'tif'
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 10), 'which page formant ?', fill = cf['color']['font'])
draw.text((10, 30), 'A4 ->', fill = cf['color']['font'])
draw.text((10, 60), 'A5 upright ->', fill = cf['color']['font'])
draw.text((10, 90), 'A5 landscape ->', fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
time.sleep(1)
papersize_x = 0
papersize_y = 0
while papersize_x == 0:
if GPIO.input(KEY1_PIN) == 0:
papersize_x = 210
papersize_y = 297
elif GPIO.input(KEY2_PIN) == 0:
papersize_x = 148
papersize_y = 210
elif GPIO.input(KEY3_PIN) == 0:
papersize_x = 210
papersize_y = 148
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 10), 'which resolution ?', fill = cf['color']['font'])
draw.text((0, 30), '150dpi (to print) ->', fill = cf['color']['font'])
draw.text((0, 60), '300dpi (documents) ->', fill = cf['color']['font'])
draw.text((0, 90), '600dpi (detailed images) ->', fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
time.sleep(1)
resolution = 0
while resolution == 0:
if GPIO.input(KEY1_PIN) == 0:
resolution = 150
elif GPIO.input(KEY2_PIN) == 0:
resolution = 300
elif GPIO.input(KEY3_PIN) == 0:
resolution = 600
jobtime = scantime()
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 0), jobtime, fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
draw.text((0, 10), 'scan ' + str(resolution) + 'dpi ' + fileformat, fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
document = cf['folder']['destination'] + "/" + cf["filename"]["prefix"] + "" + jobtime + "." + fileextension
os.system("/usr/bin/scanimage > " + document + " --format=" + fileformat + " --resolution=" + str(resolution) + " --device-name='" + cf['devicename'] + "' -x " + str(papersize_x) + " -y " + str(papersize_y))
draw.text((0, 30), 'pushover message', fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
send_to_pushover(document,jobtime,fileextension,cf)
#scan Photo
if GPIO.input(KEY2_PIN) == 0: # button is released
fileformat = 'jpeg'
fileextension = 'jpg'
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 10), 'which page formant ?', fill = cf['color']['font'])
draw.text((10, 30), 'w=13, h=9 ->', fill = cf['color']['font'])
draw.text((10, 60), 'w=15, h=10 ->', fill = cf['color']['font'])
# draw.text((10, 90), 'A5 landscape ->', fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
time.sleep(1)
papersize_x = 0
papersize_y = 0
while papersize_x == 0:
if GPIO.input(KEY1_PIN) == 0:
papersize_x = 130
papersize_y = 90
elif GPIO.input(KEY2_PIN) == 0:
papersize_x = 150
papersize_y = 100
# elif GPIO.input(KEY3_PIN) == 0:
# papersize_x = 210
# papersize_y = 148
resolution = 300
jobtime = scantime()
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 0), jobtime, fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
draw.text((0, 10), 'scan ' + str(resolution) + 'dpi ' + fileformat, fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
document = cf['folder']['destination'] + "/" + cf["filename"]["prefix"] + "" + jobtime + "." + fileextension
os.system("/usr/bin/scanimage > " + document + " --format=" + fileformat + " --resolution=" + str(resolution) + " --device-name='" + cf['devicename'] + "' -x " + str(papersize_x) + " -y " + str(papersize_y))
draw.text((0, 30), 'pushover message', fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
send_to_pushover(document,jobtime,fileextension,cf)
#scan PDF
if GPIO.input(KEY3_PIN) == 0: # button is released
biggestfilesize = 0
####scannt ganz viele JPGs mit _01.jpg
i = 0
# document_jpg = [0]
jobtime = scantime()
while True:
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 0), jobtime, fill = cf['color']['font'])
draw.text((0, 10), 'scan 300dpi pdf', fill = cf['color']['font'])
draw.text((0, 20), 'page: ' + str(i+1), fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
try:
document_jpg.append(cf['folder']['temp'] + "/" + cf["filename"]["prefix"] + "" + jobtime + "_" + str(i).zfill(2) + ".jpg")
except:
document_jpg = [cf['folder']['temp'] + "/" + cf["filename"]["prefix"] + "" + jobtime + "_" + str(i).zfill(2) + ".jpg"]
os.system("/usr/bin/scanimage > " + document_jpg[i] + " --format=jpeg --resolution=300 --device-name='" + cf['devicename'] + "' -x " + str(cf['papersize']['x']) + " -y " + str(cf['papersize']['y']))
filesize = os.path.getsize(document_jpg[i])
if filesize >= biggestfilesize: biggestfilesize = filesize
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 0), 'ready', fill = cf['color']['font'])
draw.text((32, 25), ' next', fill = cf['color']['font'])
draw.text((32, 35), ' ->', fill = cf['color']['font'])
draw.text((32, 55), ' done', fill = cf['color']['font'])
draw.text((32, 65), ' ->', fill = cf['color']['font'])
#thumbnail and counter
thumb = Image.open(document_jpg[i])
thumb_x = 100
thumb = thumb.resize((thumb_x,int(thumb.height / thumb.width * thumb_x)))
image.paste(thumb,(0,15))
draw.ellipse((5, 110, 20, 120), fill="BLACK")
draw.text((10,110), str(i+1), fill=cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
time.sleep(1)
while True:
if GPIO.input(KEY1_PIN) == 0: # button is released
option = 'next'
i=i+1
break
if GPIO.input(KEY2_PIN) == 0: # button is released
option = 'done'
break
if option == 'done':
break
#####mit ocr in eine PDF oder nur so in eine pdf
while True:
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 10), 'OCR ?', fill = cf['color']['font'])
draw.text((0, 30), ' local ->', fill = cf['color']['font'])
draw.text((0, 55), ' remote ->', fill = cf['color']['font'])
if biggestfilesize <= cf['ocrspace']['limit']:
draw.text((0, 65), ' size: ' + str(round(biggestfilesize/1024/1024,2)) + 'MB', fill = cf['color']['font'])
else:
draw.text((0, 65), ' size: ' + str(round(biggestfilesize/1024/1024,2)) + 'MB', fill = 'RED')
draw.text((0, 90), ' no ->', fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
time.sleep(1)
while True:
if GPIO.input(KEY1_PIN) == 0: # button is released
option = 'doocr'
ocroption = 'localocr'
break
if GPIO.input(KEY2_PIN) == 0: # button is released
option = 'doocr'
ocroption = 'remoteocr'
break
if GPIO.input(KEY3_PIN) == 0: # button is released
option = 'noocr'
break
if option == 'doocr' or option == 'noocr':
break
#####DO OCR
if option == 'doocr':
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 0), jobtime, fill = cf['color']['font'])
draw.text((0, 10), 'ocr pdf', fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
for file in document_jpg:
# print(file)
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 0), 'convert and ocr', fill = cf['color']['font'])
draw.text((0, 10), os.path.basename(file), fill = cf['color']['font'])
draw.text((0, 20), 'to pdf', fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
#local ocr
if ocroption == 'localocr':
print('start tesseract:' + file)
os.system("tesseract " + file + ' ' + file[0:-4] + ' -l deu+eng pdf')
#remote ocr
elif ocroption == 'remoteocr':
print('start ocrspace:' + file)
ocrspace(file)
else: print('miss ocroption value')
pdfmerge(cf,jobtime,document_jpg)
#####NO OCR
if option == 'noocr':
for file in document_jpg:
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 0), 'convert', fill = cf['color']['font'])
draw.text((0, 10), os.path.basename(file), fill = cf['color']['font'])
draw.text((0, 20), 'to pdf', fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
image_1 = Image.open(file)
im_1 = image_1.convert('RGB')
im_1.save(file[0:-4] + '.pdf', resolution=300)
pdfmerge(cf,jobtime,document_jpg)
format = 'pdf'
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.text((0, 30), 'pushover message', fill = cf['color']['font'])
disp.LCD_ShowImage(image,0,0)
send_to_pushover(document_jpg[0],jobtime,format,cf)
for file in document_jpg:
os.remove(file)
del document_jpg