Merge pull request 'CYBER' (#1) from Thijs/label_printer:master into master
Reviewed-on: #1
This commit is contained in:
commit
c961541302
8 changed files with 94 additions and 13 deletions
49
app.py
49
app.py
|
@ -3,11 +3,12 @@ from flask import Flask, render_template, request, redirect, url_for, session, s
|
|||
from PIL import Image
|
||||
from gen_image import gen_image
|
||||
from config import *
|
||||
from datetime import datetime
|
||||
from datetime import *
|
||||
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"
|
||||
|
@ -44,12 +45,25 @@ templates = {
|
|||
"pos": 130
|
||||
},
|
||||
"cut": True
|
||||
},
|
||||
"Remove by": {
|
||||
"text1": {
|
||||
"string": "Remove by",
|
||||
"size": 130,
|
||||
"pos": 0
|
||||
},
|
||||
"text2": {
|
||||
"string": "",
|
||||
"size": 50,
|
||||
"pos": 130
|
||||
},
|
||||
"cut": True
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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"])
|
||||
|
||||
|
||||
|
||||
|
@ -62,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):
|
||||
|
@ -74,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()
|
||||
|
||||
|
||||
|
||||
|
@ -140,9 +154,13 @@ def text_template():
|
|||
|
||||
if request.method == 'POST':
|
||||
template = templates[request.form["template"]]
|
||||
|
||||
if request.form["template"] == "Food":
|
||||
template["text2"]["string"] = datetime.now().strftime('%Y-%m-%d')
|
||||
|
||||
if request.form["template"] == "Remove by":
|
||||
template["text2"]["string"] = (datetime.now() + timedelta(weeks=3)).strftime('%Y-%m-%d')
|
||||
|
||||
session["text1"] = template["text1"]
|
||||
session["text2"] = template["text2"]
|
||||
session["cut"] = template["cut"]
|
||||
|
@ -166,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
20
fonts.py
Normal 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()
|
||||
}
|
10
gen_image.py
10
gen_image.py
|
@ -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)
|
||||
|
||||
|
||||
|
|
9
requirements.txt
Normal file
9
requirements.txt
Normal file
|
@ -0,0 +1,9 @@
|
|||
blinker==1.9.0
|
||||
click==8.1.7
|
||||
colorama==0.4.6
|
||||
Flask==3.1.0
|
||||
itsdangerous==2.2.0
|
||||
Jinja2==3.1.4
|
||||
MarkupSafe==3.0.2
|
||||
pillow==11.0.0
|
||||
Werkzeug==3.1.3
|
BIN
resources/OCRAEXT.TTF
Normal file
BIN
resources/OCRAEXT.TTF
Normal file
Binary file not shown.
|
@ -6,7 +6,7 @@
|
|||
<form method="POST" action="/image" enctype="multipart/form-data">
|
||||
<input type="file" name="image">
|
||||
<input type="submit" value="Generate preview"><br>
|
||||
Recommended image height = {{label_width}}, taller images will be scaled
|
||||
Recommended image height = {{label_width}}, other sizes will be scaled
|
||||
</form>
|
||||
|
||||
{% if info %}
|
||||
|
|
|
@ -8,13 +8,14 @@
|
|||
<select name="template">
|
||||
<option value="DNH">Do not hack</option>
|
||||
<option value="Food">Food</option>
|
||||
<option value="Remove by">Remove by</option>
|
||||
</select>
|
||||
<input type="submit" value="Apply">
|
||||
</form>
|
||||
|
||||
|
||||
<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
|
||||
|
@ -22,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>
|
||||
|
||||
|
|
Loading…
Reference in a new issue