0

I am using PIL on cloud function How do I point the “fonts” to that file.

I wonder shall I save the “fonts file” to the cloud storage and point the path? Any ideas or comments will be appreciated. Than you!

from PIL import Image, ImageDraw
from PIL import ImageFont
From Google.cloud import storage


storage_client = storage.Client()
. . . . . .

with blob.open() as file:
   img = Image.open(file)                 


draw = ImageDraw.Draw(img)
font = ImageFont.truetype("simsun.ttc", 18).  ## <- the font here
. . . . . . 

april
  • 53
  • 1
  • 7

1 Answers1

0

The PIL does not have the built-in ability to automatically open files from GCS. you will need to either:

  • Download the file to local storage and point PIL to that file or
  • Give PIL a BlobReader which it can use to access the data

You will have to use the reference path for the file and call the same in the code import function , example code as below:

font_file = load_font_from_gcs(gs_path)
font = ImageFont.truetype(BytesIO(font_file), 11)

Check the following helpful links with similar implementation:

Vaidehi Jamankar
  • 1,232
  • 1
  • 2
  • 10