Based on comment from @Selcuk, I found an answer as:
from PIL import ImageFont
font = ImageFont.truetype('times.ttf', 12)
size = font.getsize('Hello world')
print(size)
which prints (x, y) size as:
(58, 11)
Here it is as a function:
from PIL import ImageFont
def get_pil_text_size(text, font_size, font_name):
font = ImageFont.truetype(font_name, font_size)
size = font.getsize(text)
return size
get_pil_text_size('Hello world', 12, 'times.ttf')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…