From eb1056144c12b8933c04e83246ff8e4aa136fba5 Mon Sep 17 00:00:00 2001 From: TT-392 Date: Fri, 31 Oct 2025 14:58:23 +0000 Subject: [PATCH] modify gen_image to allow for n number of lines --- gen_image.py | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/gen_image.py b/gen_image.py index 39c82fc..3eca1c7 100644 --- a/gen_image.py +++ b/gen_image.py @@ -2,38 +2,39 @@ from PIL import Image, ImageDraw, ImageFont from colorama import Fore, Style from config import * +from fonts import fonts #font = "resources/ComicMono.ttf" -def gen_image(height, text1, text2, font): - font1 = ImageFont.truetype(font["path"], size=text1["size"]) - font2 = ImageFont.truetype(font["path"], size=text2["size"]) +def gen_image(height, lines, font): + text_widths = [] + for text in lines: + pil_font = ImageFont.truetype(font["path"], size=text["size"]) - text1["string"] = text1["string"].replace("\r\n", "\n") - text2["string"] = text2["string"].replace("\r\n", "\n") + text["string"] = text["string"].replace("\r\n", "\n") - lines1 = text1["string"].split('\n') - lines2 = text2["string"].split('\n') + sub_lines = text["string"].split('\n') - text1_width = max([font1.getbbox(line)[2] for line in lines1]) - text2_width = max([font2.getbbox(line)[2] for line in lines2]) + text_widths.append(max([pil_font.getbbox(sub_line)[2] for sub_line in sub_lines])) + text_max_start = max([pil_font.getbbox(sub_line)[0] for sub_line in sub_lines]) - text1_max_start = max([font1.getbbox(line)[0] for line in lines1]) - text2_max_start = max([font2.getbbox(line)[0] for line in lines2]) + # I am assuming the left corner of the bbox to always be 0, I have yet to + # encounter a situation when this isn't the case + if (text_max_start != 0): + print(Fore.YELLOW + "Warning, found situation where left corner of bbox of text != 0,", "text1_box:", text1, "text2_box", text2) - # I am assuming the left corner of the bbox to always be 0, I have yet to - # encounter a situation when this isn't the case - if (text1_max_start != 0 or text2_max_start != 0): - print(Fore.YELLOW + "Warning, found situation where left corner of bbox of text != 0,", "text1_box:", text1, "text2_box", text2) # the 1 here assures we don't get a 0 width image - width = max(text1_width, text2_width, 1) + text_widths.append(1) + width = max(text_widths) + # '1' is 1 bit bitdepth img = Image.new('1', (width, height), color=1) draw = ImageDraw.Draw(img) - draw.text((0, text1["pos"]), text1["string"], font=font1, stroke_width = font["stroke_width_bold"] if text1["bold"] else font["stroke_width"], stroke_fill="black") - draw.text((0, text2["pos"]), text2["string"], font=font2, stroke_width = font["stroke_width_bold"] if text2["bold"] else font["stroke_width"], stroke_fill="black") + for line in lines: + pil_font = ImageFont.truetype(font["path"], size=text["size"]) + draw.text((0, line["pos"]), line["string"], font=pil_font, stroke_width = font["stroke_width_bold"] if line["bold"] else font["stroke_width"], stroke_fill="black") if (width > max_label_length): @@ -49,11 +50,22 @@ Text1 = { "string": "Hello world", "size": 30, "pos": 10, + "bold": False, } Text2 = { "string": "Hello worhfeiheifhefld\nafefe", "size": 20, "pos": 40, + "bold": False, } +Text3 = { + "string": "Hello worhfeiheifhefld\nafefe", + "size": 20, + "pos": 80, + "bold": False, +} + +gen_image(200, [Text1, Text2, Text3], fonts()["CYBER"])[2].save("aaa.png") +