Python, PIL; Text to Image and fonts
Asked Answered
M

2

22

I have an issue with writing text to an image under Python and PIL - I'm able to write text to a png file, though not bold text. Could anyone provide an example of how to achieve this?

I thought the easiest solution may be was use a bold-variant of a text, but I'm unable to see anything in the Windows/font folder that supplies this - does this mean font types have a 'bold attribute' that is T/F?:
quick look for bold-fonts under windows

Code I'm using:

import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

# font = ImageFont.truetype("Arial-Bold.ttf",14)
font = ImageFont.truetype("Arial.ttf",14)
img=Image.new("RGBA", (500,250),(255,255,255))
draw = ImageDraw.Draw(img)
draw.text((0, 0),"This is a test",(0,0,0),font=font)
draw = ImageDraw.Draw(img)
img.save("a_test.png")
Mastoid answered 6/4, 2013 at 23:19 Comment(0)
P
13

You aren't looking at actual font files in the control panel (explorer magically turns into the font viewer control panel when in the Windows/fonts folder as well), they are grouped by family for your convenience. Double click the family to see the fonts in the family:

enter image description here

Then right-click and choose properties to find the file name:

enter image description here

Paola answered 6/4, 2013 at 23:39 Comment(1)
Thanks Pavel, that worked fine: font = ImageFont.truetype("Arialbd.ttf",14)Mastoid
E
17

A simple way to do it:

font = ImageFont.load_default().font

Also you can do a google search for 'verdana.ttf' and download it put it in the same directory as the python file:

Then add it like this:

font = ImageFont.truetype("Verdana.ttf",14)
Enos answered 15/11, 2015 at 9:24 Comment(0)
P
13

You aren't looking at actual font files in the control panel (explorer magically turns into the font viewer control panel when in the Windows/fonts folder as well), they are grouped by family for your convenience. Double click the family to see the fonts in the family:

enter image description here

Then right-click and choose properties to find the file name:

enter image description here

Paola answered 6/4, 2013 at 23:39 Comment(1)
Thanks Pavel, that worked fine: font = ImageFont.truetype("Arialbd.ttf",14)Mastoid

© 2022 - 2024 — McMap. All rights reserved.