CYBER #1

Merged
emilevs merged 4 commits from Thijs/label_printer:master into master 2024-12-04 20:29:16 +00:00
7 changed files with 67 additions and 12 deletions
Showing only changes of commit 4e2ffb0224 - Show all commits

30
app.py
View file

@ -8,6 +8,7 @@ from print import *
from process_image import *
from file_handler import *
from colorama import Fore, Style
from fonts import fonts
import os
text_image_filename = "text_image.png"
@ -62,7 +63,7 @@ templates = {
def render_text_template(info=None, info_color=None, scrollDown=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, scrollDown=scrollDown)
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, scrollDown=scrollDown, fonts=session["fonts"])
@ -75,7 +76,6 @@ def base():
check_for_new_user(session)
return render_template('base.html')
# gets triggered when the user_data folder for the user is created
@new_user_handler
def on_new_user(session):
@ -87,6 +87,7 @@ def on_new_user(session):
session["text1"] = templates["DNH"]["text1"]
session["text2"] = templates["DNH"]["text2"]
session["cut"] = templates["DNH"]["cut"]
session["fonts"] = fonts()
@ -183,7 +184,30 @@ def text_form():
"pos": int(request.form["pos2"]),
}
message, status, img = gen_image(label_width, session["text1"], session["text2"])
# 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
message, status, img = gen_image(label_width, session["text1"], session["text2"], chosen_font)
if status == "Error":
session["text image path"] = None

20
fonts.py Normal file
View file

@ -0,0 +1,20 @@
class Font:
def __init__(self, name: str, path: str, stroke_width: int, default: bool = False) -> None:
self.name = name
self.path = path
self.stroke_width = stroke_width
self.selected = default
def to_dict(self):
return {
"name": self.name,
"path": self.path,
"selected": self.selected,
"stroke_width": self.stroke_width
}
def fonts():
return {
"CYBER": Font("CYBER", "resources/OCRAEXT.TTF", 3, True).to_dict(),
"ComicMono": Font("Comic Sans Mono", "resources/ComicMono.ttf", 0).to_dict()
}

View file

@ -3,11 +3,11 @@ from PIL import Image, ImageDraw, ImageFont
from colorama import Fore, Style
from config import *
font = "resources/ComicMono.ttf"
#font = "resources/ComicMono.ttf"
def gen_image(height, text1, text2):
font1 = ImageFont.truetype(font, size=text1["size"])
font2 = ImageFont.truetype(font, size=text2["size"])
def gen_image(height, text1, text2, font):
font1 = ImageFont.truetype(font["path"], size=text1["size"])
font2 = ImageFont.truetype(font["path"], size=text2["size"])
text1["string"] = text1["string"].replace("\r\n", "\n")
text2["string"] = text2["string"].replace("\r\n", "\n")
@ -33,7 +33,7 @@ def gen_image(height, text1, text2):
draw = ImageDraw.Draw(img)
draw.text((0, text1["pos"]), text1["string"], font=font1)
draw.text((0, text1["pos"]), text1["string"], font=font1, stroke_width=font["stroke_width"], stroke_fill="black")
draw.text((0, text2["pos"]), text2["string"], font=font2)

View file

@ -6,7 +6,8 @@ import ctypes
import io
import time
printer = ctypes.CDLL('./epson/library_bridge.so')
#printer = ctypes.CDLL('./epson/library_bridge.so')
printer = None
def print_text(text):
time.sleep(0.1)

BIN
resources/OCRAEXT.TTF Normal file

Binary file not shown.

View file

@ -15,7 +15,7 @@
<form method="POST" action="/text-form">
<h2>String 1</h2>
<h2>Top text</h2>
Text:<br>
<textarea name="string1">{{text1["string"]}}</textarea><br>
Size: <input type="number" value="{{text1["size"]}}" name="size1" min="0" required
@ -23,13 +23,23 @@
Y position: <input type="number" value="{{text1["pos"]}}" name="pos1" required
style="width: 40px"><br>
<h2>String 2</h2>
<h2>Bottom text</h2>
Text:<br>
<textarea name="string2">{{text2["string"]}}</textarea><br>
Size: <input type="number" value="{{text2["size"]}}" name="size2" min="0" required
style="width: 40px"><br>
Y position: <input type="number" value="{{text2["pos"]}}" name="pos2" required
style="width: 40px"><br>
style="width: 40px">
<h2>Font</h2>
<label for="font">Font:</label>
<select name="font">
{% for font in fonts %}
<option value="{{ font }}"{% if fonts[font].selected %} selected{% endif %}>{{ fonts[font].name }}</option>
{% endfor %}
</select>
<br />
<input type="submit" value="Generate preview">
</form>