added variable font

This commit is contained in:
weckyy702
2020-12-27 16:12:22 +01:00
parent 688c4ad0ac
commit 2e70191b04
3 changed files with 23 additions and 14 deletions

View File

@@ -1,11 +1,13 @@
from typing import Iterable
from PIL import Image, ImageDraw, ImageFont
import os
datei_endung = ".png"
img = None
output_img = None
font = ImageFont.truetype("res/fonts/NotoSans-Regular.ttf", 12)
_bkp_font = ImageFont.truetype("res/fonts/NotoSans-Regular.ttf", 12) #in case set_font does funky stuff, backup the original font
font = _bkp_font
def NSD_init(x: float, y: float):
@@ -15,6 +17,16 @@ def NSD_init(x: float, y: float):
img = Image.new("RGB", (x, y), "white")
output_img = ImageDraw.Draw(img)
def set_font(font_filepath: str):
if not os.path.exists(font_filepath):
raise FileNotFoundError("")
try:
font = ImageFont.truetype(font_filepath)
except:
font = _bkp_font
raise
def get_text_size(text: str):
if not font:
raise Exception("Output image was not initialized! Make sure to call NSD_init first")