From a6163cff0a9ebd38a6844c1e01dbf0c55e3e31d7 Mon Sep 17 00:00:00 2001 From: TT-392 Date: Sat, 14 Sep 2024 15:29:37 +0200 Subject: [PATCH] Working flask server with simple image upload --- .gitignore | 3 +++ flask/app.py | 46 ++++++++++++++++++++++++++++++++++++++ flask/templates/base.html | 35 +++++++++++++++++++++++++++++ flask/templates/image.html | 19 ++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 flask/app.py create mode 100644 flask/templates/base.html create mode 100644 flask/templates/image.html diff --git a/.gitignore b/.gitignore index 140f8cf..5dbf259 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ *.so +*.swp +*/__pycache__ +flask/static diff --git a/flask/app.py b/flask/app.py new file mode 100644 index 0000000..ccedd6c --- /dev/null +++ b/flask/app.py @@ -0,0 +1,46 @@ +from flask import Flask, render_template, request, redirect, url_for +from PIL import Image +import os + +valid_height=800 + +app = Flask(__name__) +app.config['UPLOAD_FOLDER'] = 'static/uploads' + +# Ensure the upload directory exists +if not os.path.exists(app.config['UPLOAD_FOLDER']): + os.makedirs(app.config['UPLOAD_FOLDER']) + +@app.route('/', methods=['GET', 'POST']) +def upload_image(): + if request.method == 'POST': + if 'image' not in request.files: + return redirect(request.url) + file = request.files['image'] + + if file.filename == '': + return redirect(request.url) + if file: + extension = os.path.splitext(file.filename)[1] + filepath = os.path.join(app.config['UPLOAD_FOLDER'], "upload" + extension) + + file.save(filepath) + + # Process image # + height = Image.open(filepath).height + + valid = False + if height == valid_height: + valid = True + ################# + + return render_template('image.html', filename="upload"+extension, height=height, valid=valid) + return render_template('image.html', filename=None) + +@app.route('/uploads/') +def uploaded_file(filename): + return redirect(url_for('static', filename='uploads/' + filename)) + +if __name__ == "__main__": + app.run(debug=True) + diff --git a/flask/templates/base.html b/flask/templates/base.html new file mode 100644 index 0000000..b926191 --- /dev/null +++ b/flask/templates/base.html @@ -0,0 +1,35 @@ + + + + + + Image Upload + + + + + +
+
Text
+
Image
+
QR
+
Barcode
+
+ + {% block content %} + {% endblock %} + + + + diff --git a/flask/templates/image.html b/flask/templates/image.html new file mode 100644 index 0000000..68d9179 --- /dev/null +++ b/flask/templates/image.html @@ -0,0 +1,19 @@ +{% extends "base.html" %} + +{% block content %} +

Upload Image

+
+ + +
+ {% if filename %} +

Uploaded Image:

+ Uploaded Image + {% if valid %} +

Height: {{ height }} px

+ {% else %} +

Height: {{ height }} px

+ {% endif %} + {% endif %} +{% endblock %} +