-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
44 lines (33 loc) · 1.57 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
from flask import Flask, render_template, request, flash, session
from flask_bootstrap import Bootstrap
import os
from werkzeug.utils import secure_filename
from forms import ExtractColoursForm
from colour_extractor import ColourExtractor
app = Flask(__name__)
Bootstrap(app)
app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY")
@app.route("/", methods=["POST", "GET"])
def home():
colour_form = ExtractColoursForm()
if colour_form.validate_on_submit():
# Remove all previous flash messages
session.pop("_flashes", None)
# Save uploaded image ------------------------------------------------------------------------------------------
file_name = secure_filename(colour_form.file.data.filename)
file = colour_form.file.data
path = "static/img/" + file_name
file.save(path)
print(path)
# Retrieve form data -------------------------------------------------------------------------------------------
colours_num = int(request.form["colours_num"])
# Create ColourExtractor object --------------------------------------------------------------------------------
extractor = ColourExtractor()
extractor.load_image(path)
extractor.resize_image()
colours = extractor.extract_colours(colours_num)
flash("Colours extracted successfully.")
return render_template("colours.html", form=colour_form, colours=colours, image=path)
return render_template("colours.html", form=colour_form, colours=None, image=None)
if __name__ == '__main__':
app.run(debug=True)