[英]Can't load font on Python Image Library (pillow)
我必須在圖像上寫一些文字。 我正在使用 PIL,但我無法在此圖片上設置特定字體。 我嘗試使用下面的代碼來做到這一點,但似乎字體和字體大小不受我的代碼的影響。
PS:字體文件在python腳本的同一目錄下
#text => text to write on .jpeg
def writeImage(text):
img = Image.new('RGB', (1920, 1080), color = (255,255,255))
draw = ImageDraw.Draw(img)
#load font
fnt = ImageFont.truetype("constan.ttf", 36)
#write text and save image
draw.text((100,100), text, fill=(0,0,0))
img.save("recap.jpg")
繪制文本時,需要具體告訴它使用哪種字體:
# note the font=fnt at the end
draw.text((100, 100), text, fill=(0, 0, 0), font=fnt)
通過這種方式,您可以輕松加載數十或數百種字體,並為每段文本選擇應使用哪種字體進行渲染。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.