From 025c1ec9b006026e5030f250ab79d4d07683a698 Mon Sep 17 00:00:00 2001 From: TT-392 Date: Thu, 26 Sep 2024 21:19:11 +0200 Subject: [PATCH] Handle pillow file errors and move intermediate file storage to ram --- app.py | 28 ++++++++++------------------ process_image.py | 4 ++-- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/app.py b/app.py index afe897e..5f9dd82 100644 --- a/app.py +++ b/app.py @@ -10,8 +10,8 @@ from file_handler import * from colorama import Fore, Style import os -generated_image_filename = "generated.png" -uploaded_image_filename = "uploaded" #extention is depended on uploaded file format +text_image_filename = "text_image.png" +uploaded_image_filename = "uploaded_image.png" app = Flask(__name__) @@ -90,20 +90,21 @@ def image(): return redirect(request.url) if file: extension = os.path.splitext(file.filename)[1] - session["uploaded image path"] = uploaded_image_filename+extension - filepath = get_file_path(session, session["uploaded image path"]) - file.save(filepath) + try: + message, status, img = process_image(file.stream) + session["uploaded image path"] = uploaded_image_filename + img.save(get_file_path(session, session["uploaded image path"])) + except Exception as e: + message = f"{e}" + status = "Error" - message, status, img = process_image(filepath) - img.save(filepath) if status == "Error": session["uploaded image path"] = None return render_image_template("Error: " + message, "red") elif status == "Info": - session["text image path"] = None return render_image_template("Info: " + message, "black") return render_image_template() @@ -173,7 +174,7 @@ def text_form(): get_folder_path(session) - session["text image path"] = generated_image_filename + session["text image path"] = text_image_filename img.save(get_file_path(session, session["text image path"])) print() print(session["text image path"]) @@ -216,15 +217,6 @@ def user_data(filename): return send_from_directory(get_folder_path(session), filename) -@app.route('/uploads/') -def uploaded_file(filename): - return redirect(url_for('static', filename='uploads/' + filename)) - - -@app.route('/text/') -def generated_file(filename): - return redirect(url_for('static', filename='text/' + filename)) - if __name__ == "__main__": app.run(debug=True) diff --git a/process_image.py b/process_image.py index cd426ad..2b60b76 100644 --- a/process_image.py +++ b/process_image.py @@ -11,8 +11,8 @@ def format_image_to_label(path): return new_image -def process_image(path): - image = Image.open(path) +def process_image(image_filestream): + image = Image.open(image_filestream) message = None status = None