Implement label length restrictions, and label width recommendation

This commit is contained in:
TT-392 2024-09-26 20:38:41 +02:00
parent 10c0fd71f0
commit ae9ac584f3
8 changed files with 78 additions and 24 deletions

47
app.py
View file

@ -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/<filename>')
@ -206,5 +227,5 @@ def generated_file(filename):
if __name__ == "__main__":
app.run(debug=False)
app.run(debug=True)

View file

@ -2,3 +2,5 @@
label_width = 213
printer_width = 512
max_label_length = 2000

View file

@ -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')

View file

@ -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

View file

@ -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")

View file

@ -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 */

View file

@ -5,11 +5,15 @@
<h1>Upload Image</h1>
<form method="POST" action="/image" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="Generate preview">
<input type="submit" value="Generate preview"><br>
Recommended image height = {{label_width}}, taller images will be scaled
</form>
{% if info %}
<p class="info" style="color: {{info_color}}">{{info}}</p>
{% endif %}
{% if filename %}
<br><br>
<img src="user_data/{{filename}}" alt="Uploaded image" style="height: 150px; width: auto;">
<form method="POST" action="/image-print">

View file

@ -32,8 +32,11 @@
<input type="submit" value="Generate preview">
</form>
{% if info %}
<p class="info" style="color: {{info_color}}">{{info}}</p>
{% endif %}
{% if filename %}
<br><br>
<img src="user_data/{{filename}}" alt="Generated Image" style="height: 150px; width: auto;">
<form method="POST" action="/text-print">
@ -48,5 +51,6 @@
</form>
{% endif %}
{% endblock %}