diff --git a/app.py b/app.py index 0a1dbdc..afe897e 100644 --- a/app.py +++ b/app.py @@ -2,10 +2,10 @@ from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory from PIL import Image from gen_image import gen_image -from printer_info import * +from config import * from datetime import datetime from print import * -from format_image import * +from process_image import * from file_handler import * from colorama import Fore, Style import os @@ -48,6 +48,15 @@ templates = { } +def render_text_template(info=None, info_color=None): + return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"], info=info, info_color=info_color) + + + +def render_image_template(info=None, info_color=None): + return render_template('image.html', filename=session["uploaded image path"], cut=session["cut"], info=info, info_color=info_color, label_width=label_width) + + @app.route('/', methods=['GET', 'POST']) def base(): check_for_new_user(session) @@ -86,9 +95,18 @@ def image(): filepath = get_file_path(session, session["uploaded image path"]) file.save(filepath) - format_image(filepath).save(filepath) + 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_template('image.html', filename=session["uploaded image path"], cut=session["cut"]) + return render_image_template() #TODO: maybe merge some stuff with text-print @@ -112,7 +130,7 @@ def image_print(): print("printing") session["cut"] = False; - return render_template('image.html', filename=session["uploaded image path"], cut=session["cut"]) + return render_image_template() @app.route('/text-template', methods=['GET', 'POST']) @@ -128,7 +146,7 @@ def text_template(): session["text2"] = template["text2"] session["cut"] = template["cut"] - return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"]) + return render_text_template() @app.route('/text-form', methods=['GET', 'POST']) @@ -147,7 +165,11 @@ def text_form(): "pos": int(request.form["pos2"]), } - img = gen_image(label_width, session["text1"], session["text2"]) + message, status, img = gen_image(label_width, session["text1"], session["text2"]) + + if status == "Error": + session["text image path"] = None + return render_text_template(message, "red") get_folder_path(session) @@ -156,14 +178,14 @@ def text_form(): print() print(session["text image path"]) - return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"]) + return render_text_template() @app.route('/text', methods=['GET', 'POST']) def text(): check_for_new_user(session) - return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"]) + return render_text_template() @app.route('/text-print', methods=['GET', 'POST']) @@ -173,8 +195,7 @@ def text_print(): if request.method == 'POST': if session["text image path"] == None: print(Fore.YELLOW + "Warning, file doesn't exist" + Style.RESET_ALL) - return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"]) - + return render_text_template() image = format_image_to_label(get_file_path(session, session["text image path"])) print_image(image) @@ -187,7 +208,7 @@ def text_print(): print("printing") session["cut"] = False; - return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"]) + return render_text_template() @app.route('/user_data/') @@ -206,5 +227,5 @@ def generated_file(filename): if __name__ == "__main__": - app.run(debug=False) + app.run(debug=True) diff --git a/printer_info.py b/config.py similarity index 76% rename from printer_info.py rename to config.py index 4edbee5..eca4c42 100644 --- a/printer_info.py +++ b/config.py @@ -2,3 +2,5 @@ label_width = 213 printer_width = 512 + +max_label_length = 2000 diff --git a/gen_image.py b/gen_image.py index be8c7df..ec2d3a1 100644 --- a/gen_image.py +++ b/gen_image.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later from PIL import Image, ImageDraw, ImageFont from colorama import Fore, Style -from printer_info import * +from config import * font = "resources/ComicMono.ttf" @@ -27,14 +27,24 @@ def gen_image(height, text1, text2): print(Fore.YELLOW + "Warning, found situation where left corner of bbox of text != 0,", "text1_box:", text1, "text2_box", text2) # the 1 here assures we don't get a 0 width image - img = Image.new('1', (max(text1_width, text2_width, 1), height), color=1) + width = max(text1_width, text2_width, 1) + + img = Image.new('1', (width, height), color=1) draw = ImageDraw.Draw(img) draw.text((0, text1["pos"]), text1["string"], font=font1) draw.text((0, text2["pos"]), text2["string"], font=font2) - return img + + if (width > max_label_length): + message = f"Label too long, max allowed length = {max_label_length}, yours = {width}." + status = "Error" + else: + message = None + status = None + + return message, status, img Text1 = { "string": "Hello world", @@ -48,4 +58,3 @@ Text2 = { "pos": 40, } -gen_image(label_width, Text1, Text2).save('output.png') diff --git a/print.py b/print.py index 4813502..6f94cea 100644 --- a/print.py +++ b/print.py @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-3.0-or-later dev_path="/dev/usb/lp0" -from format_image import * +from process_image import * from PIL import Image import ctypes import io diff --git a/format_image.py b/process_image.py similarity index 62% rename from format_image.py rename to process_image.py index 6e5f597..cd426ad 100644 --- a/format_image.py +++ b/process_image.py @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-3.0-or-later from PIL import Image -from printer_info import * +from config import * def format_image_to_label(path): image = Image.open(path) @@ -11,10 +11,15 @@ def format_image_to_label(path): return new_image -def format_image(path): +def process_image(path): image = Image.open(path) + message = None + status = None if (image.height != label_width): + message = f"image height = {image.height} pixels, will be resized to fit {label_width}" + status = "Info" + print("resizing image") new_height = label_width new_width = int(label_width * image.width / image.height) @@ -22,7 +27,12 @@ def format_image(path): image = image.resize((new_width, new_height)) image = image.convert('1', dither=Image.FLOYDSTEINBERG) - return image + + if image.width > max_label_length: + message= f"Label too long, max allowed length = {max_label_length}, yours = {image.width}." + status = "Error" + + return message, status, image #format_image("static/uploads/upload.png").save("output.png") diff --git a/templates/base.html b/templates/base.html index 4c410ec..c737d0a 100644 --- a/templates/base.html +++ b/templates/base.html @@ -28,6 +28,10 @@ border: 2px solid #55cdfc; /* Light blue */ transition: background-color 0.3s ease; } + + .info { + font-size: 20px; + } .box:hover { background-color: #f7a8b8; /* Softer pink on hover */ diff --git a/templates/image.html b/templates/image.html index e6224ff..1a4f2f5 100644 --- a/templates/image.html +++ b/templates/image.html @@ -5,11 +5,15 @@

Upload Image

- +
+ Recommended image height = {{label_width}}, taller images will be scaled
+ {% if info %} +

{{info}}

+ {% endif %} + {% if filename %} -

Uploaded image
diff --git a/templates/text.html b/templates/text.html index 1fe8df8..bcb3f1b 100644 --- a/templates/text.html +++ b/templates/text.html @@ -32,8 +32,11 @@
+ {% if info %} +

{{info}}

+ {% endif %} + {% if filename %} -

Generated Image
@@ -48,5 +51,6 @@
{% endif %} + {% endblock %}