FedericoAntosiano
FedericoAntosiano

Reputation: 25

Can't load font on Python Image Library (pillow)

I have to write some text over an image. I'm using PIL, but I can't set the specific font on this picture. I tried to do it using the code below, but it seems that the font and the font size aren't affected by my code.

PS: the font file is in the same directory of the python script

#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")

Upvotes: 2

Views: 531

Answers (1)

blubberdiblub
blubberdiblub

Reputation: 4135

When drawing text, you need to tell it specifically which font to use:

# note the font=fnt at the end
draw.text((100, 100), text, fill=(0, 0, 0), font=fnt)

This way, you can easily load up tens or hundreds of fonts and choose for each piece of text which font it should be rendered with.

Upvotes: 1

Related Questions