label_printer/app.py

229 lines
7 KiB
Python
Raw Normal View History

2024-09-25 19:48:36 +00:00
# SPDX-License-Identifier: GPL-3.0-or-later
from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory
from PIL import Image
2024-09-14 18:49:57 +00:00
from gen_image import gen_image
from config import *
2024-11-27 22:55:50 +00:00
from datetime import *
2024-09-15 10:41:12 +00:00
from print import *
from process_image import *
from file_handler import *
from colorama import Fore, Style
2024-11-27 23:59:36 +00:00
from fonts import fonts
import os
text_image_filename = "text_image.png"
uploaded_image_filename = "uploaded_image.png"
app = Flask(__name__)
2024-09-14 18:49:57 +00:00
app.secret_key = "blahaj"
2024-09-14 18:49:57 +00:00
def render_text_template(info=None, info_color=None, scrollDown=None):
2025-10-31 18:54:10 +00:00
return render_template('text.html', filename=session["text image path"], cut=session["cut"], info=info, info_color=info_color, scrollDown=scrollDown, fonts=session["fonts"])
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)
2024-09-16 15:45:11 +00:00
@app.route('/', methods=['GET', 'POST'])
def base():
check_for_new_user(session)
2024-12-04 21:26:50 +00:00
return render_template('default.html')
2024-09-16 15:45:11 +00:00
# gets triggered when the user_data folder for the user is created
@new_user_handler
def on_new_user(session):
print(Fore.BLUE + "new user with ID:", session["ID"], Style.RESET_ALL)
session["text image path"] = None
session["uploaded image path"] = None
session["cut"] = True
2025-10-31 18:54:10 +00:00
session["cut"] = True
2024-11-27 23:59:36 +00:00
session["fonts"] = fonts()
2025-11-02 18:51:07 +00:00
class Image_preset:
def __init__(self, path, dithering=True, threshold_if_not_dithering=0.5):
self.path = path
self.dithering = dithering
self.threshold_if_not_dithering = threshold_if_not_dithering
2025-11-02 14:50:49 +00:00
image_presets = {
2025-11-02 18:51:07 +00:00
"Abandoned": Image_preset("resources/abandoned.jpg", False, 0.5)
2025-11-02 14:50:49 +00:00
}
2024-09-14 18:49:57 +00:00
@app.route('/image', methods=['GET', 'POST'])
def image():
check_for_new_user(session)
if request.method == 'POST':
if 'image' not in request.files:
return render_image_template()
2025-11-02 14:50:49 +00:00
if "dropdown" in request.form and request.form["dropdown"] != "file":
2025-11-02 18:51:07 +00:00
path = image_presets[request.form["dropdown"]].path
dither = image_presets[request.form["dropdown"]].dithering
threshold_if_not_dithering = image_presets[request.form["dropdown"]].threshold_if_not_dithering
2025-11-02 14:50:49 +00:00
class file:
filename = os.path.basename(path)
stream = open(path, "rb")
else:
file = request.files['image']
2025-11-02 18:51:07 +00:00
dither = "dithering" in request.form
threshold_if_not_dithering = float(request.form["threshold"]) * 0.01
2025-11-02 14:50:49 +00:00
if file.filename == '':
return render_image_template()
2025-11-02 14:50:49 +00:00
else:
extension = os.path.splitext(file.filename)[1]
try:
2025-11-02 18:51:07 +00:00
message, status, img = process_image(file.stream, dither, threshold_if_not_dithering)
2025-11-02 14:50:49 +00:00
file.stream.close()
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"
if status == "Error":
session["uploaded image path"] = None
return render_image_template("Error: " + message, "red")
elif status == "Info":
return render_image_template("Info: " + message, "black")
2024-09-15 12:57:26 +00:00
return render_image_template()
2024-09-15 12:57:26 +00:00
#TODO: maybe merge some stuff with text-print
@app.route('/image-print', methods=['GET', 'POST'])
2024-09-16 15:45:11 +00:00
def image_print():
check_for_new_user(session)
if session["uploaded image path"] == None:
print(Fore.YELLOW + "Warning, file doesn't exist" + Style.RESET_ALL)
return render_template('image.html', filename=session["uploaded image path"], cut=session["cut"])
2024-09-15 12:57:26 +00:00
if request.method == 'POST':
image = format_image_to_label(get_file_path(session, session["uploaded image path"]))
2024-09-15 12:57:26 +00:00
print_image(image)
if 'cut' in request.form:
print("printing and cutting")
cut_paper()
session["cut"] = True;
else:
print("printing")
session["cut"] = False;
return render_image_template()
2024-09-14 18:49:57 +00:00
2024-09-14 18:49:57 +00:00
@app.route('/text-form', methods=['GET', 'POST'])
def text_form():
check_for_new_user(session)
2024-09-14 18:49:57 +00:00
if request.method == 'POST':
2025-10-31 15:31:00 +00:00
line_count = int(request.form["lineCount"])
lines = []
for i in range(0, line_count):
lines.append({
"string": request.form["text" + str(i)],
"size": int(request.form["size" + str(i)]),
"pos": int(request.form["y_pos" + str(i)]),
"bold": "bold" + str(i) in request.form
2025-10-31 15:31:00 +00:00
})
print(request.form)
2025-10-31 15:31:00 +00:00
# Clear previously saved font
for font in session["fonts"]:
session["fonts"][font]["selected"] = False
# If an invalid font has been submitted, just default to the first one
chosen_font = request.form["font"]
if chosen_font in session["fonts"]:
session["fonts"][chosen_font]["selected"] = True
else:
first_font = next(iter(session["fonts"]))
session["fonts"][first_font]["selected"] = True
# Select the first font that has been marked as selected
chosen_font = None
for font in session["fonts"]:
f = session["fonts"][font]
if f["selected"]:
chosen_font = f
# If the font is still None, something has gone wrong
if chosen_font is None:
return
print(chosen_font)
message, status, img = gen_image(label_width, lines, chosen_font)
if status == "Error":
session["text image path"] = None
return render_text_template(message, "red")
get_folder_path(session)
session["text image path"] = text_image_filename
img.save(get_file_path(session, session["text image path"]))
print()
print(session["text image path"])
2024-09-14 18:49:57 +00:00
return render_text_template(scrollDown = True)
2024-09-14 18:49:57 +00:00
@app.route('/text', methods=['GET', 'POST'])
def text():
check_for_new_user(session)
return render_text_template()
2024-09-14 18:49:57 +00:00
@app.route('/text-print', methods=['GET', 'POST'])
2024-09-16 15:45:11 +00:00
def text_print():
check_for_new_user(session)
2024-09-14 18:49:57 +00:00
if request.method == 'POST':
if session["text image path"] == None:
print(Fore.YELLOW + "Warning, file doesn't exist" + Style.RESET_ALL)
return render_text_template()
image = format_image_to_label(get_file_path(session, session["text image path"]))
2024-09-15 10:41:12 +00:00
print_image(image)
2024-09-14 18:49:57 +00:00
if 'cut' in request.form:
print("printing and cutting")
2024-09-15 10:41:12 +00:00
cut_paper()
2024-09-14 18:49:57 +00:00
session["cut"] = True;
else:
print("printing")
session["cut"] = False;
return render_text_template()
2024-09-14 18:49:57 +00:00
@app.route('/user_data/<filename>')
def user_data(filename):
return send_from_directory(get_folder_path(session), filename)
2024-09-14 18:49:57 +00:00
if __name__ == "__main__":
app.run(debug=True)