-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
86 lines (43 loc) · 1.75 KB
/
Main.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
from flask import Flask, flash, request, redirect, render_template
from werkzeug.utils import secure_filename
from PIL import Image
import io
import base64
app = Flask(__name__)
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def upload_form():
return render_template('upload.html')
@app.route('/', methods=['POST'])
def upload_image():
images = []
for file in request.files.getlist("file[]"):
print("***************************")
print("image: ", file)
if file.filename == '':
flash('No image selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filestr = file.read()
img = Image.open(io.BytesIO(filestr))
width, height = img.size
ratio = height / 500.0
img = img.resize((int(width / ratio), 500), Image.ANTIALIAS)
gray = img.convert('L')
fm = cv2.Laplacian(np.array(gray), cv2.CV_64F).var()
result = "Not Blurry"
if fm < 100:
result = "Blurry"
sharpness_value = "{:.0f}".format(fm)
message = [result,sharpness_value]
file_object = io.BytesIO()
img.save(file_object, format='PNG')
base64img = "data:image/png;base64,"+base64.b64encode(file_object.getvalue()).decode('ascii')
images.append([message,base64img])
print("images:", len(images))
return render_template('upload.html', images=images )
if __name__ == "__main__":
app.run(debug=True)