├── .gitattributes ├── main.py ├── static ├── 2.jpg ├── 2.png ├── 2.webp └── hZ7ntRp.webp ├── templates ├── about.html └── index.html └── uploads ├── 2.png └── hZ7ntRp.png /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # pip3 install flask opencv-python 2 | from flask import Flask, render_template, request, flash 3 | from werkzeug.utils import secure_filename 4 | import cv2 5 | import os 6 | 7 | UPLOAD_FOLDER = 'uploads' 8 | ALLOWED_EXTENSIONS = {'png', 'webp', 'jpg', 'jpeg', 'gif'} 9 | 10 | app = Flask(__name__) 11 | app.secret_key = 'super secret key' 12 | app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER 13 | 14 | def allowed_file(filename): 15 | return '.' in filename and \ 16 | filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS 17 | 18 | def processImage(filename, operation): 19 | print(f"the operation is {operation} and filename is {filename}") 20 | img = cv2.imread(f"uploads/{filename}") 21 | match operation: 22 | case "cgray": 23 | imgProcessed = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 24 | newFilename = f"static/{filename}" 25 | cv2.imwrite(newFilename, imgProcessed) 26 | return newFilename 27 | case "cwebp": 28 | newFilename = f"static/{filename.split('.')[0]}.webp" 29 | cv2.imwrite(newFilename, img) 30 | return newFilename 31 | case "cjpg": 32 | newFilename = f"static/{filename.split('.')[0]}.jpg" 33 | cv2.imwrite(newFilename, img) 34 | return newFilename 35 | case "cpng": 36 | newFilename = f"static/{filename.split('.')[0]}.png" 37 | cv2.imwrite(newFilename, img) 38 | return newFilename 39 | pass 40 | 41 | @app.route("/") 42 | def home(): 43 | return render_template("index.html") 44 | 45 | @app.route("/about") 46 | def about(): 47 | return render_template("about.html") 48 | 49 | @app.route("/edit", methods=["GET", "POST"]) 50 | def edit(): 51 | if request.method == "POST": 52 | operation = request.form.get("operation") 53 | # check if the post request has the file part 54 | if 'file' not in request.files: 55 | flash('No file part') 56 | return "error" 57 | file = request.files['file'] 58 | # If the user does not select a file, the browser submits an 59 | # empty file without a filename. 60 | if file.filename == '': 61 | flash('No selected file') 62 | return "error no selected file" 63 | if file and allowed_file(file.filename): 64 | filename = secure_filename(file.filename) 65 | file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) 66 | new = processImage(filename, operation) 67 | flash(f"Your image has been processed and is available here") 68 | return render_template("index.html") 69 | 70 | return render_template("index.html") 71 | 72 | 73 | app.run(debug=True, port=5001) -------------------------------------------------------------------------------- /static/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithHarry/flask-image-editor/9f6a2d7f05ca676ef8b29bcde02d9eedbc070ec0/static/2.jpg -------------------------------------------------------------------------------- /static/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithHarry/flask-image-editor/9f6a2d7f05ca676ef8b29bcde02d9eedbc070ec0/static/2.png -------------------------------------------------------------------------------- /static/2.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithHarry/flask-image-editor/9f6a2d7f05ca676ef8b29bcde02d9eedbc070ec0/static/2.webp -------------------------------------------------------------------------------- /static/hZ7ntRp.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeWithHarry/flask-image-editor/9f6a2d7f05ca676ef8b29bcde02d9eedbc070ec0/static/hZ7ntRp.webp -------------------------------------------------------------------------------- /templates/about.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 |