33

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

jack
  • 17,261
  • 37
  • 100
  • 125

6 Answers6

29

Use the bold/italic version of the font

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 16
    Just to clarify: each version of the typeface comes in a separate file, eg: Arial.ttf, ArialBold.ttf, etc.. – btk Feb 29 '12 at 16:43
  • If you are using mac, you can find font file from Font Book.app. Just right click on font name, then click "Show in Finder". You can check the file path, you drag the font file on Terminal.app. – Nori May 12 '21 at 12:03
10

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")
Feisal Aswad
  • 365
  • 3
  • 9
9

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')
zara
  • 109
  • 1
  • 5
6

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

djc
  • 11,603
  • 5
  • 41
  • 54
3

Well, this is my first comment. Here we go.

I'll try to clarify the procedure. At first What I did was use the "name" of the font like this

font = ImageFont.truetype("C:\Windows\Fonts\\Arial Negrita.ttf",25)

but only got some errors like this:

    Traceback (most recent call last):
  File "C:/Users/555STi/PycharmProjects/PIL/img.py", line 8, in <module>
    font = ImageFont.truetype("C:\Windows\Fonts\Arial negrita.ttf",25)
  File "C:\Python27\lib\site-packages\PIL\ImageFont.py", line 262, in truetype
    return FreeTypeFont(font, size, index, encoding)
  File "C:\Python27\lib\site-packages\PIL\ImageFont.py", line 142, in __init__
    self.font = core.getfont(font, size, index, encoding)
IOError: cannot open resource

Then I remembered that sometimes fonts has other "names" or "filenames", so, what I did was going to fonts folder, then opened the Arial Font wich displayed all the styles like negrita (bold], cursiva(italic), etc.

Did a right click on the "negrita" style, selected "properties" and then there was the "real name" of the font.

In my case, the name was "ariblk"

Then, finally, just used the name like this.

font = ImageFont.truetype("C:\Windows\Fonts\\ariblk.ttf",25)

I know this post is old, but today helped me to get to the solution. So I hope to help anybody.

=)

0

With reference to the other answers here, my search for the name for the bold variant of Arial produced the following (arialbd.ttf):

def FindFontsVariantsWithBase(fontBase="arial"):
        import matplotlib
        system_fonts = matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
        # for font in np.sort(system_fonts):
        #     print(font)
        fonts = np.sort(system_fonts).tolist()
        res = [i for i in fonts if fontBase in i]
        print(res)
        
FindFontsVariantsWithBase("arial")

['C:\WINDOWS\Fonts\arial.ttf', 'C:\WINDOWS\Fonts\arialbd.ttf', 'C:\WINDOWS\Fonts\arialbi.ttf', 'C:\WINDOWS\Fonts\ariali.ttf', 'C:\Windows\Fonts\arial.ttf', 'C:\Windows\Fonts\arialbd.ttf', 'C:\Windows\Fonts\arialbi.ttf', 'C:\Windows\Fonts\ariali.ttf'] (

HackJob99
  • 89
  • 5