-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
273 lines (233 loc) · 9.1 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from flask import Flask, request, render_template, jsonify, redirect, url_for, send_from_directory
from PIL import Image, ImageDraw, ImageFont
from html.parser import HTMLParser
from brother_ql.raster import BrotherQLRaster
from brother_ql.conversion import convert
from brother_ql.backends import backend_factory
import os
import json
import logging
app = Flask(__name__)
SETTINGS_FILE = "settings.json"
UPLOAD_FOLDER = "uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
DEFAULT_SETTINGS = {
"printer_uri": "tcp://192.168.1.100",
"printer_model": "QL-800",
"label_size": "62",
"font_size": 50,
"alignment": "left",
"rotate": "0",
"threshold": 70.0,
"dither": False,
"compress": False,
"red": False
}
logging.basicConfig(level=logging.DEBUG)
def load_settings():
if os.path.exists(SETTINGS_FILE):
with open(SETTINGS_FILE, "r") as f:
return json.load(f)
return DEFAULT_SETTINGS
def save_settings(settings):
with open(SETTINGS_FILE, "w") as f:
json.dump(settings, f, indent=4)
settings = load_settings()
class TextParser(HTMLParser):
def __init__(self):
super().__init__()
self.parts = []
def handle_starttag(self, tag, attrs):
if tag == "br":
self.parts.append("<br>")
def handle_data(self, data):
self.parts.append(data)
def handle_endtag(self, tag):
pass
@app.route("/")
def index():
"""
Renders the main page of the Web-GUI.
"""
return render_template("index.html", settings=settings)
@app.route("/settings", methods=["GET"])
def get_settings():
"""
Returns current settings in JSON format.
"""
return jsonify(settings)
@app.route("/update_settings", methods=["POST"])
def update_settings():
"""
Updates settings from the Web-GUI form and saves them.
"""
global settings
for key in DEFAULT_SETTINGS.keys():
if key in request.form:
value = request.form[key]
if isinstance(DEFAULT_SETTINGS[key], bool):
settings[key] = value.lower() == "true"
elif isinstance(DEFAULT_SETTINGS[key], int):
settings[key] = int(value)
elif isinstance(DEFAULT_SETTINGS[key], float):
settings[key] = float(value)
else:
settings[key] = value
save_settings(settings)
return redirect(url_for("index"))
@app.route("/api/text/", methods=["POST"])
def api_text():
data = request.get_json()
if not data or "text" not in data or "settings" not in data:
logging.error("No text or settings provided")
return jsonify({"error": "No text or settings provided"}), 400
text = data["text"]
settings = load_settings()
local_settings = {
"printer_uri": data["settings"].get("printer_uri", settings["printer_uri"]),
"printer_model": data["settings"].get("printer_model", settings["printer_model"]),
"label_size": data["settings"].get("label_size", settings["label_size"]),
"font_size": data["settings"].get("font_size", settings["font_size"]),
"alignment": data["settings"].get("alignment", settings["alignment"]),
"rotate": data["settings"].get("rotate", settings["rotate"]),
"threshold": data["settings"].get("threshold", settings["threshold"]),
"dither": data["settings"].get("dither", settings["dither"]),
"red": data["settings"].get("red", settings["red"]),
"compress": data["settings"].get("compress", settings["compress"]),
}
try:
logging.info("Received text: %s", text)
logging.info("Using settings: %s", local_settings)
image_path = create_label_image(text, local_settings)
logging.info("Image created at: %s", image_path)
if local_settings["rotate"] != "0":
image_path = apply_rotation(image_path, int(local_settings["rotate"]))
logging.info("Image rotated to: %s", image_path)
send_to_printer(image_path, local_settings)
logging.info("Text printed successfully")
return jsonify({"success": True})
except Exception as e:
logging.error(f"Error printing text: {e}")
return jsonify({"error": str(e)}), 500
@app.route("/api/image/", methods=["POST"])
def print_image():
"""
Endpoint to print images. Accepts an uploaded image and settings via request.
"""
if "image" not in request.files:
return jsonify({"error": "Kein Bild hochgeladen."}), 400
image_file = request.files["image"]
if image_file.filename == "":
return jsonify({"error": "Kein Bild ausgewählt."}), 400
try:
# Parse optional settings from request
api_settings = json.loads(request.form.get("settings", "{}"))
local_settings = {**settings, **api_settings} # Combine global settings with API-provided settings
# Save the uploaded image
image_path = os.path.join(UPLOAD_FOLDER, image_file.filename)
image_file.save(image_path)
# Resize the image to fit label width
resized_path = resize_image(image_path)
# Apply rotation if specified
rotated_path = apply_rotation(resized_path, int(local_settings.get("rotate", 0)))
# Send to printer with updated settings
send_to_printer(rotated_path, local_settings)
return jsonify({"success": True, "message": "Bild erfolgreich gedruckt!"})
except Exception as e:
logging.error(f"Fehler beim Bilddruck: {e}")
return jsonify({"error": str(e)}), 500
@app.route('/locales/<path:filename>')
def serve_locale(filename):
return send_from_directory('locales', filename)
def create_label_image(html_text, local_settings):
width = 696 # Fixed label width
font_size = int(local_settings.get("font_size", 50))
alignment = local_settings.get("alignment", "left")
parser = TextParser()
parser.feed(html_text)
lines = []
current_line = []
for part in parser.parts:
if part == "<br>":
lines.append("".join(current_line))
current_line = []
else:
current_line.append(part)
if current_line:
lines.append("".join(current_line))
dummy_image = Image.new("RGB", (width, 10), "white")
dummy_draw = ImageDraw.Draw(dummy_image)
total_height = 10
line_spacing = 5
line_metrics = []
for line in lines:
font = ImageFont.truetype(FONT_PATH, font_size)
bbox = dummy_draw.textbbox((0, 0), line, font=font)
line_width = bbox[2] - bbox[0]
line_height = bbox[3] - bbox[1]
max_ascent, max_descent = font.getmetrics()
total_height += line_height + line_spacing
line_metrics.append((line, max_ascent, max_descent, line_height, line_width))
total_height += 10
image = Image.new("RGB", (width, total_height), "white")
draw = ImageDraw.Draw(image)
y = 10
for line_text, max_ascent, max_descent, line_height, line_width in line_metrics:
if alignment == "center":
x = (width - line_width) // 2
elif alignment == "right":
x = width - line_width - 10
else:
x = 10
draw.text((x, y), line_text, font=font, fill="black")
y += line_height + line_spacing
image_path = os.path.join(UPLOAD_FOLDER, "text_label.png")
image.save(image_path)
return image_path
def apply_rotation(image_path, angle):
"""
Applies rotation to the label image.
"""
with Image.open(image_path) as img:
rotated_img = img.rotate(-angle, resample=Image.Resampling.LANCZOS, expand=True)
rotated_path = os.path.join(UPLOAD_FOLDER, f"rotated_{os.path.basename(image_path)}")
rotated_img.save(rotated_path)
return rotated_path
def resize_image(image_path):
"""
Resizes an uploaded image to fit the label width.
"""
max_width = 696
with Image.open(image_path) as img:
aspect_ratio = img.height / img.width
new_height = int(max_width * aspect_ratio)
img = img.resize((max_width, new_height), Image.Resampling.LANCZOS)
resized_path = os.path.join(UPLOAD_FOLDER, "resized_" + os.path.basename(image_path))
img.save(resized_path)
return resized_path
def send_to_printer(image_path, local_settings):
"""
Sends the label image to the printer with the provided settings.
"""
try:
qlr = BrotherQLRaster(local_settings["printer_model"])
qlr.exception_on_warning = True
instructions = convert(
qlr=qlr,
images=[image_path],
label=local_settings["label_size"],
rotate=local_settings["rotate"],
threshold=float(local_settings["threshold"]),
dither=local_settings["dither"],
compress=local_settings["compress"],
red=local_settings["red"],
)
backend = backend_factory("network")["backend_class"](local_settings["printer_uri"])
backend.write(instructions)
backend.dispose()
except Exception as e:
logging.error(f"Fehler beim Drucken: {e}")
raise
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)