Skip to content

Commit

Permalink
fix image print
Browse files Browse the repository at this point in the history
  • Loading branch information
Dodoooh committed Dec 15, 2024
1 parent 220918f commit ce6847a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
20 changes: 2 additions & 18 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from flask import Flask, request, render_template, jsonify
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
Expand All @@ -9,45 +8,38 @@

app = Flask(__name__)

# Standard-Dateipfad für Settings und Uploads
SETTINGS_FILE = "settings.json"
UPLOAD_FOLDER = "uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)

FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"

# Standard-Einstellungen
DEFAULT_SETTINGS = {
"printer_uri": "tcp://192.168.1.100",
"printer_model": "QL-800",
"label_size": "62",
"font_size": 50,
"alignment": "left",
"rotate": "0", # Optionen: "0", "90", "180", "270"
"rotate": "0",
"threshold": 70.0,
"dither": False,
"compress": False,
"red": True
"red": False
}

# Einstellungen laden/speichern
def load_settings():
"""Lädt die Einstellungen aus einer JSON-Datei."""
if os.path.exists(SETTINGS_FILE):
with open(SETTINGS_FILE, "r") as f:
return json.load(f)
return DEFAULT_SETTINGS.copy()

def save_settings(settings):
"""Speichert die Einstellungen in eine JSON-Datei."""
with open(SETTINGS_FILE, "w") as f:
json.dump(settings, f, indent=4)

# Globale Einstellungen initialisieren
settings = load_settings()

class TextParser(HTMLParser):
"""Parst HTML und speichert Text mit Formatierungen."""
def __init__(self):
super().__init__()
self.parts = []
Expand Down Expand Up @@ -78,12 +70,10 @@ def index():

@app.route("/settings", methods=["GET"])
def get_settings():
"""Gibt die gespeicherten Einstellungen zurück."""
return jsonify(settings)

@app.route("/update_settings", methods=["POST"])
def update_settings():
"""Einstellungen aktualisieren und persistent speichern."""
global settings
settings["printer_uri"] = request.form.get("printer_uri", settings["printer_uri"])
settings["printer_model"] = request.form.get("printer_model", settings["printer_model"])
Expand All @@ -95,13 +85,11 @@ def update_settings():
settings["dither"] = request.form.get("dither", str(settings["dither"])).lower() == "true"
settings["compress"] = request.form.get("compress", str(settings["compress"])).lower() == "true"
settings["red"] = request.form.get("red", str(settings["red"])).lower() == "true"

save_settings(settings)
return jsonify({"success": True, "message": "Einstellungen gespeichert."})

@app.route("/api/text/", methods=["POST"])
def api_text():
"""Verarbeitet und druckt den eingegebenen Text."""
data = request.json
text = data.get("text", "").strip()
alignment = data.get("settings", {}).get("alignment", settings["alignment"])
Expand All @@ -117,7 +105,6 @@ def api_text():
return jsonify({"error": str(e)}), 500

def create_label_image(html_text, alignment="left"):
"""Erstellt ein Labelbild mit dynamischer Höhe."""
width = 696
parser = TextParser()
parser.feed(html_text)
Expand Down Expand Up @@ -187,7 +174,6 @@ def create_label_image(html_text, alignment="left"):

@app.route("/api/image/", methods=["POST"])
def print_image():
"""Verarbeitet und druckt hochgeladene Bilder."""
if "image" not in request.files:
return jsonify({"error": "Kein Bild hochgeladen."}), 400

Expand All @@ -205,7 +191,6 @@ def print_image():
return jsonify({"error": str(e)}), 500

def resize_image(image_path):
"""Passt die Größe des Bildes an die Druckbreite an."""
max_width = 696
with Image.open(image_path) as img:
aspect_ratio = img.height / img.width
Expand All @@ -216,7 +201,6 @@ def resize_image(image_path):
return resized_path

def send_to_printer(image_path):
"""Sendet das Bild oder Label an den Drucker."""
qlr = BrotherQLRaster(settings["printer_model"])
qlr.exception_on_warning = True
instructions = convert(
Expand Down
2 changes: 2 additions & 0 deletions static/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ document.addEventListener("DOMContentLoaded", () => {
document.getElementById("label_size").value = settings.label_size || "62";

// Druckeinstellungen
document.getElementById("font_size").value = settings.font_size || 50;
document.getElementById("rotate").value = settings.rotate || "0";
document.getElementById("threshold").value = settings.threshold || "70";
document.getElementById("dither").value = settings.dither ? "true" : "false";
Expand All @@ -61,6 +62,7 @@ document.addEventListener("DOMContentLoaded", () => {
formData.append("label_size", document.getElementById("label_size").value);

// Druckeinstellungen
formData.append("font_size", document.getElementById("font_size").value);
formData.append("rotate", document.getElementById("rotate").value);
formData.append("threshold", document.getElementById("threshold").value);
formData.append("dither", document.getElementById("dither").value);
Expand Down

0 comments on commit ce6847a

Please sign in to comment.