-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPythonScannerOCR.py
More file actions
65 lines (57 loc) · 1.99 KB
/
PythonScannerOCR.py
File metadata and controls
65 lines (57 loc) · 1.99 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
import flask
from flask import request
from PIL import Image
from io import BytesIO
import base64
import pytesseract
import os
UPLOAD_FOLDER = 'C:\\Program Files\\Tesseract-OCR\\tesseract'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app = flask.Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'imagefile' not in request.files:
# flash('No file part')
return redirect(request.url)
file = request.files['imagefile']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
# flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = file.filename
file.save(filename)
return "<h1> Worked " + str(filename) + "</h1>"
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<p><input type=file name=imagefile>
<input type=submit value=Upload>
</form>
'''
@app.route('/upload', methods=['POST'])
def upload():
print("hello")
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract'
imagefile = request.form.get('imagefile')
im = Image.open(BytesIO(base64.b64decode(imagefile)))
w, h = im.size
im.save('temp.png', 'PNG')
#im.save("temp.png")
text=pytesseract.image_to_string(Image.open("temp.png"))
file2write=open("writefile.txt",'wb')
file2write.write(bytes(text, 'UTF-8'))
file2write.close()
return "<h1> Worked " + str(w) + "</h1>"
if __name__ == "__main__":
#app.run(host='0.0.0.0')
app.run(debug=True,host='0.0.0.0')