Implement label length restrictions, and label width recommendation
This commit is contained in:
parent
10c0fd71f0
commit
ae9ac584f3
8 changed files with 78 additions and 24 deletions
47
app.py
47
app.py
|
@ -2,10 +2,10 @@
|
||||||
from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory
|
from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from gen_image import gen_image
|
from gen_image import gen_image
|
||||||
from printer_info import *
|
from config import *
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from print import *
|
from print import *
|
||||||
from format_image import *
|
from process_image import *
|
||||||
from file_handler import *
|
from file_handler import *
|
||||||
from colorama import Fore, Style
|
from colorama import Fore, Style
|
||||||
import os
|
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'])
|
@app.route('/', methods=['GET', 'POST'])
|
||||||
def base():
|
def base():
|
||||||
check_for_new_user(session)
|
check_for_new_user(session)
|
||||||
|
@ -86,9 +95,18 @@ def image():
|
||||||
filepath = get_file_path(session, session["uploaded image path"])
|
filepath = get_file_path(session, session["uploaded image path"])
|
||||||
file.save(filepath)
|
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
|
#TODO: maybe merge some stuff with text-print
|
||||||
|
@ -112,7 +130,7 @@ def image_print():
|
||||||
print("printing")
|
print("printing")
|
||||||
session["cut"] = False;
|
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'])
|
@app.route('/text-template', methods=['GET', 'POST'])
|
||||||
|
@ -128,7 +146,7 @@ def text_template():
|
||||||
session["text2"] = template["text2"]
|
session["text2"] = template["text2"]
|
||||||
session["cut"] = template["cut"]
|
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'])
|
@app.route('/text-form', methods=['GET', 'POST'])
|
||||||
|
@ -147,7 +165,11 @@ def text_form():
|
||||||
"pos": int(request.form["pos2"]),
|
"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)
|
get_folder_path(session)
|
||||||
|
|
||||||
|
@ -156,14 +178,14 @@ def text_form():
|
||||||
print()
|
print()
|
||||||
print(session["text image path"])
|
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'])
|
@app.route('/text', methods=['GET', 'POST'])
|
||||||
def text():
|
def text():
|
||||||
check_for_new_user(session)
|
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'])
|
@app.route('/text-print', methods=['GET', 'POST'])
|
||||||
|
@ -173,8 +195,7 @@ def text_print():
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
if session["text image path"] == None:
|
if session["text image path"] == None:
|
||||||
print(Fore.YELLOW + "Warning, file doesn't exist" + Style.RESET_ALL)
|
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"]))
|
image = format_image_to_label(get_file_path(session, session["text image path"]))
|
||||||
print_image(image)
|
print_image(image)
|
||||||
|
@ -187,7 +208,7 @@ def text_print():
|
||||||
print("printing")
|
print("printing")
|
||||||
session["cut"] = False;
|
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>')
|
@app.route('/user_data/<filename>')
|
||||||
|
@ -206,5 +227,5 @@ def generated_file(filename):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(debug=False)
|
app.run(debug=True)
|
||||||
|
|
||||||
|
|
|
@ -2,3 +2,5 @@
|
||||||
|
|
||||||
label_width = 213
|
label_width = 213
|
||||||
printer_width = 512
|
printer_width = 512
|
||||||
|
|
||||||
|
max_label_length = 2000
|
17
gen_image.py
17
gen_image.py
|
@ -1,7 +1,7 @@
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
from PIL import Image, ImageDraw, ImageFont
|
from PIL import Image, ImageDraw, ImageFont
|
||||||
from colorama import Fore, Style
|
from colorama import Fore, Style
|
||||||
from printer_info import *
|
from config import *
|
||||||
|
|
||||||
font = "resources/ComicMono.ttf"
|
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)
|
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
|
# 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 = ImageDraw.Draw(img)
|
||||||
|
|
||||||
draw.text((0, text1["pos"]), text1["string"], font=font1)
|
draw.text((0, text1["pos"]), text1["string"], font=font1)
|
||||||
draw.text((0, text2["pos"]), text2["string"], font=font2)
|
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 = {
|
Text1 = {
|
||||||
"string": "Hello world",
|
"string": "Hello world",
|
||||||
|
@ -48,4 +58,3 @@ Text2 = {
|
||||||
"pos": 40,
|
"pos": 40,
|
||||||
}
|
}
|
||||||
|
|
||||||
gen_image(label_width, Text1, Text2).save('output.png')
|
|
||||||
|
|
2
print.py
2
print.py
|
@ -1,6 +1,6 @@
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
dev_path="/dev/usb/lp0"
|
dev_path="/dev/usb/lp0"
|
||||||
from format_image import *
|
from process_image import *
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import ctypes
|
import ctypes
|
||||||
import io
|
import io
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from printer_info import *
|
from config import *
|
||||||
|
|
||||||
def format_image_to_label(path):
|
def format_image_to_label(path):
|
||||||
image = Image.open(path)
|
image = Image.open(path)
|
||||||
|
@ -11,10 +11,15 @@ def format_image_to_label(path):
|
||||||
|
|
||||||
return new_image
|
return new_image
|
||||||
|
|
||||||
def format_image(path):
|
def process_image(path):
|
||||||
image = Image.open(path)
|
image = Image.open(path)
|
||||||
|
message = None
|
||||||
|
status = None
|
||||||
|
|
||||||
if (image.height != label_width):
|
if (image.height != label_width):
|
||||||
|
message = f"image height = {image.height} pixels, will be resized to fit {label_width}"
|
||||||
|
status = "Info"
|
||||||
|
|
||||||
print("resizing image")
|
print("resizing image")
|
||||||
new_height = label_width
|
new_height = label_width
|
||||||
new_width = int(label_width * image.width / image.height)
|
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.resize((new_width, new_height))
|
||||||
|
|
||||||
image = image.convert('1', dither=Image.FLOYDSTEINBERG)
|
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")
|
#format_image("static/uploads/upload.png").save("output.png")
|
|
@ -28,6 +28,10 @@
|
||||||
border: 2px solid #55cdfc; /* Light blue */
|
border: 2px solid #55cdfc; /* Light blue */
|
||||||
transition: background-color 0.3s ease;
|
transition: background-color 0.3s ease;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.info {
|
||||||
|
font-size: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
.box:hover {
|
.box:hover {
|
||||||
background-color: #f7a8b8; /* Softer pink on hover */
|
background-color: #f7a8b8; /* Softer pink on hover */
|
||||||
|
|
|
@ -5,11 +5,15 @@
|
||||||
<h1>Upload Image</h1>
|
<h1>Upload Image</h1>
|
||||||
<form method="POST" action="/image" enctype="multipart/form-data">
|
<form method="POST" action="/image" enctype="multipart/form-data">
|
||||||
<input type="file" name="image">
|
<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>
|
</form>
|
||||||
|
|
||||||
|
{% if info %}
|
||||||
|
<p class="info" style="color: {{info_color}}">{{info}}</p>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{% if filename %}
|
{% if filename %}
|
||||||
<br><br>
|
|
||||||
<img src="user_data/{{filename}}" alt="Uploaded image" style="height: 150px; width: auto;">
|
<img src="user_data/{{filename}}" alt="Uploaded image" style="height: 150px; width: auto;">
|
||||||
|
|
||||||
<form method="POST" action="/image-print">
|
<form method="POST" action="/image-print">
|
||||||
|
|
|
@ -32,8 +32,11 @@
|
||||||
<input type="submit" value="Generate preview">
|
<input type="submit" value="Generate preview">
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
{% if info %}
|
||||||
|
<p class="info" style="color: {{info_color}}">{{info}}</p>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{% if filename %}
|
{% if filename %}
|
||||||
<br><br>
|
|
||||||
<img src="user_data/{{filename}}" alt="Generated Image" style="height: 150px; width: auto;">
|
<img src="user_data/{{filename}}" alt="Generated Image" style="height: 150px; width: auto;">
|
||||||
|
|
||||||
<form method="POST" action="/text-print">
|
<form method="POST" action="/text-print">
|
||||||
|
@ -48,5 +51,6 @@
|
||||||
</form>
|
</form>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue