Draw bold/italic text with PIL?

后端 未结 4 1967
悲&欢浪女
悲&欢浪女 2021-01-07 18:37

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

4条回答
  •  囚心锁ツ
    2021-01-07 19:06

    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 
        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.

    =)

提交回复
热议问题