-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
108 lines (84 loc) · 3.3 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from flask import Flask ,render_template , flash, request , send_file
import numpy as np
from werkzeug.utils import secure_filename
import os
import cv2
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'webp', 'png', '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}"
case "cwebp":
newFilename = f"static/{filename.split('.')[0]}.webp"
case "cjpg":
newFilename = f"static/{filename.split('.')[0]}.jpg"
case "cpng":
newFilename = f"static/{filename.split('.')[0]}.png"
case "blur":
imgProcessed = cv2.GaussianBlur(img, (15, 15), 0)
newFilename = f"static/{filename.split('.')[0]}_blur.jpg"
case "sharpen":
kernel = np.array([[0, -1, 0],
[-1, 5,-1],
[0, -1, 0]])
imgProcessed = cv2.filter2D(img, -1, kernel)
newFilename = f"static/{filename.split('.')[0]}_sharpen.jpg"
case "edge":
imgProcessed = cv2.Canny(img, 100, 200)
newFilename = f"static/{filename.split('.')[0]}_edge.jpg"
if operation in {"cgray", "blur", "sharpen", "edge"}:
cv2.imwrite(newFilename, imgProcessed)
else:
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("/how")
def how():
return render_template("how.html")
@app.route("/contact")
def contact():
return render_template("contact.html")
STATIC_FOLDER = 'static'
@app.route('/download/<filename>')
def download(filename):
path = os.path.join(STATIC_FOLDER, filename)
return send_file(path, as_attachment=True)
@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)
if new:
flash(f"Your image has been processed and is available <a href='/{new}' target ='_blank'> here</a>")
return render_template("index.html", new_filename = new)
return render_template("contact.html")
app.run(debug=True , port =5001)