51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
from colorama import Fore, Style
|
|
from printer_info import *
|
|
|
|
font = "resources/ComicMono.ttf"
|
|
|
|
def gen_image(height, text1, text2):
|
|
font1 = ImageFont.truetype(font, size=text1["size"])
|
|
font2 = ImageFont.truetype(font, size=text2["size"])
|
|
|
|
text1["string"] = text1["string"].replace("\r\n", "\n")
|
|
text2["string"] = text2["string"].replace("\r\n", "\n")
|
|
|
|
lines1 = text1["string"].split('\n')
|
|
lines2 = text2["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])
|
|
|
|
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 (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
|
|
img = Image.new('1', (max(text1_width, text2_width, 1), height), color=1)
|
|
|
|
draw = ImageDraw.Draw(img)
|
|
|
|
draw.text((0, text1["pos"]), text1["string"], font=font1)
|
|
draw.text((0, text2["pos"]), text2["string"], font=font2)
|
|
|
|
return img
|
|
|
|
Text1 = {
|
|
"string": "Hello world",
|
|
"size": 30,
|
|
"pos": 10,
|
|
}
|
|
|
|
Text2 = {
|
|
"string": "Hello worhfeiheifhefld\nafefe",
|
|
"size": 20,
|
|
"pos": 40,
|
|
}
|
|
|
|
gen_image(label_width, Text1, Text2).save('output.png')
|