-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
72 lines (62 loc) · 2.5 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
from flask import Flask, render_template, request, flash
from werkzeug.utils import secure_filename
import cv2
import os
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'png', 'webp', 'jpg', 'jpeg', 'gif'}
app = Flask(__name__)
app.secret_key = 'super secret key'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def processImage(filename, operation):
print(f"the operation is {operation} and filename is {filename}")
img = cv2.imread(f"uploads/{filename}")
match operation:
case "cgray":
imgProcessed = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
newFilename = f"static/{filename}"
cv2.imwrite(newFilename, imgProcessed)
return newFilename
case "cwebp":
newFilename = f"static/{filename.split('.')[0]}.webp"
cv2.imwrite(newFilename, img)
return newFilename
case "cjpg":
newFilename = f"static/{filename.split('.')[0]}.jpg"
cv2.imwrite(newFilename, img)
return newFilename
case "cpng":
newFilename = f"static/{filename.split('.')[0]}.png"
cv2.imwrite(newFilename, img)
return newFilename
pass
@app.route("/")
def home():
return render_template("index.html")
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/edit", methods=["GET", "POST"])
def edit():
if request.method == "POST":
operation = request.form.get("operation")
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return "error"
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
flash('No selected file')
return "error no selected file"
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
new = processImage(filename, operation)
flash(f"Your image has been processed and is available <a href='/{new}' target='_blank'>here</a>")
return render_template("index.html")
return render_template("index.html")
app.run(debug=True, port=5001)