diff --git a/app.py b/app.py index d51509d..367f0f7 100644 --- a/app.py +++ b/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 diff --git a/fonts.py b/fonts.py new file mode 100644 index 0000000..19f4f2b --- /dev/null +++ b/fonts.py @@ -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() + } diff --git a/gen_image.py b/gen_image.py index ec2d3a1..ba060e2 100644 --- a/gen_image.py +++ b/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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fa29020 --- /dev/null +++ b/requirements.txt @@ -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 diff --git a/resources/LICENSE b/resources/LICENSE-ComicMono similarity index 100% rename from resources/LICENSE rename to resources/LICENSE-ComicMono diff --git a/resources/OCRAEXT.TTF b/resources/OCRAEXT.TTF new file mode 100644 index 0000000..0e77e20 Binary files /dev/null and b/resources/OCRAEXT.TTF differ diff --git a/templates/image.html b/templates/image.html index 1a4f2f5..c23ffe6 100644 --- a/templates/image.html +++ b/templates/image.html @@ -6,7 +6,7 @@

- Recommended image height = {{label_width}}, taller images will be scaled + Recommended image height = {{label_width}}, other sizes will be scaled
{% if info %} diff --git a/templates/text.html b/templates/text.html index bcb3f1b..364fdd9 100644 --- a/templates/text.html +++ b/templates/text.html @@ -8,13 +8,14 @@
-

String 1

+

Top text

Text:

Size:
-

String 2

+

Bottom text

Text:

Size:
Y position:
+ style="width: 40px"> + +

Font

+ + + +