Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw bold/italic text with PIL?

How to draw bold/italic text with PIL? ImageFont.truetype(file, size) has an option to specify font size only.

like image 912
jack Avatar asked Nov 29 '09 10:11

jack


4 Answers

Use the bold/italic version of the font

like image 80
John La Rooy Avatar answered Oct 16 '22 01:10

John La Rooy


A rather hacky solution to make a font bold if (for whatever reason) you don't have a separate bold version of the font is to print the same text several times with a slight offset.

andaleMono = ImageFont.truetype(ANDALE_MONO_PATH,16)
text = "hello world"
mainOffset = (50,50)
xoff, yoff = mainOffset
draw.text(mainOffset,text,font=andaleMono,fill='black')
draw.text((xoff+1,yoff+1),text,font=andaleMono,fill='black')
draw.text((xoff-1,yoff-1),text,font=andaleMono,fill='black')
like image 24
zara Avatar answered Oct 16 '22 01:10

zara


There is no bold features as parameter till now but easily you can solve by add stroke to the text with same color of text. it will make sense as bold font next code elaborate how to use stroke

    draw.text((x, y), text, fill=color, font=font, stroke_width=2,
          stroke_fill="black")
like image 8
Feisal Aswad Avatar answered Oct 16 '22 01:10

Feisal Aswad


Many fonts use different TTF files for their bold/italic versions, so I'd imagine if you just specify that file it would work.

like image 6
djc Avatar answered Oct 16 '22 01:10

djc