-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
79 lines (55 loc) · 2.27 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
import os
from flask import Flask, flash, request, redirect, url_for, render_template
from werkzeug.utils import secure_filename #uploading file method of Flask
import cv2
UPLOAD_FOLDER = 'upload'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
app = Flask(__name__, template_folder='template')
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):
image = cv2.imread(f"upload/{filename}")
match operation:
case "gray":
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
newFilename = f"static/{filename}"
cv2.imwrite(newFilename, image_gray)
return newFilename
case "cjpg":
newFilename = f"static/{filename.split('.')[0] + '.jpg'}"
cv2.imwrite(newFilename, image)
return newFilename
case "cpng":
newFilename = f"static/{filename.split('.')[0] + '.png'}"
cv2.imwrite(newFilename, image)
return newFilename
case "cwebp":
newFilename = f"static/{filename.split('.')[0] + '.webp'}"
cv2.imwrite(newFilename, image)
return newFilename
@app.route('/')
def starter():
return render_template("webpage.html")
@app.route("/edit", methods = ["GET", "POST"])
def edit():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
operation = request.form['operation']
# 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 redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
newFilename = processImage(filename, operation)
return f"Your file <a href= '{newFilename}'> here </a> "
return ''''''
app.run(debug= True)