Fonts for Pillow

Are there any fonts apart from “load_default.font()” that I can use for PIL’s Imagefont?

If not, how could I upload fonts? I’m guessing I could load them as media into a Data Table?

EDIT -

answered my own question, yes I can use Data Tables. Example to follow …

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 :

  1. upload the ttf file to a media field in a Data Table
  2. retrieve the file and store it in a BytesIO buffer
  3. 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)
4 Likes