Ok, Pillow ImageFont.load() requires a font file. As far as I can tell I cannot rely on anything stored on the file system of the Anvil servers (eg /tmp) being kept so here’s what I did. This might be blindingly obvious to some, but as it’s new to me I thought I would share :
- upload the ttf file to a media field in a Data Table
- retrieve the file and store it in a BytesIO buffer
- use that buffer as the file for the load()
The code :
def get_font(fname="Arial"):
row=app_tables.fonts.get(font_name=fname)
return row['font_data'].get_bytes()
def my_image_processing_routine():
font_file_stream=io.BytesIO(get_font("Arial"))
font = ImageFont.truetype(font_file_stream,20)