Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font Error When Drawing Text to an Image

I'm writing a script that takes my grades from my school grade website and draws the grades onto an image and saves it as my background and I'm trying to change the font when i draw the text onto the image but I'm getting an error

Here is the code I'm trying:

font = ImageFont.load('C:\WINDOWS\Fonts\CALIBRI.TTF')
img = Image.open('bg.bmp')
draw = ImageDraw.Draw(img)

now = datetime.datetime.now()


draw.text((625, 425),'                      CURRENT GRADES' )
draw.text((625, 475), 'Period 1: Geography -----------------------------{0}'.format(a),("blue"),(font))
draw.text((625, 525), 'Period 2: Francais-------------------------------{0}'.format(b),("red"),(font))
draw.text((625, 575), 'Period 3: Science--------------------------------{0}'.format(c),("orange"),(font))
draw.text((625, 625), 'Period 4: P.E------------------------------------{0}'.format(d),("blue"),(font))
draw.text((625, 675), 'Period 5: Algebra 9------------------------------{0}'.format(e),("red"),(font))
draw.text((625, 725), 'Period 6: LA-------------------------------------{0}'.format(f),("orange"),(font))
draw.text((625, 775), 'Last Updated: {0}'.format(now))

img.save('mod_bg.bmp')

but when i do this i get this error message:

Traceback (most recent call last):
  File "C:\Python27\Project.py", line 45, in <module>
     font = ImageFont.load('C:\WINDOWS\Fonts\CALIBRI.TTF')
  File "C:\Python27\lib\site-packages\PIL\ImageFont.py", line 193, in load
    f._load_pilfont(filename)
  File "C:\Python27\lib\site-packages\PIL\ImageFont.py", line 91, in _load_pilfont
    raise IOError("cannot find glyph data file")
IOError: cannot find glyph data file

can anyone tell me why this isn't working and what i should do instead

like image 938
Serial Avatar asked May 07 '13 00:05

Serial


2 Answers

The load() and load_path() functions should only be used for bitmap font files. Since you're trying to load a TrueType font (.ttf), you should use the truetype() function.

from PIL import ImageFont
font = ImageFont.truetype(r'C:\WINDOWS\Fonts\CALIBRI.TTF')

The PIL / Pillow libraries may have changed since the posting of your question, so I suggest you consult the appropriate documentation for the library you're using.

Side Note - Be careful when using backslashes in string literals (especially Windows file paths). The backslash is an escape character. For example, '\n' is a single newline character, not two characters. You should either escape each of your backslashes with another backslash ('C:\\WINDOWS\\Fonts\\CALIBRI.TTF') or use a "raw" string literal to prevent backslashes from being interpreted (r'C:\WINDOWS\Fonts\CALIBRI.TTF').

When in doubt, print() it out.

like image 73
xikkub Avatar answered Sep 22 '22 19:09

xikkub


I ran across this error and I believe it is because your installed PIL was compiled without libfreetype. To get this you'd have to install freetype2 and then compile PIL. See

Python: The _imagingft C module is not installed

Alternatively you could try Pillow, which is an updated fork of the PIL library

https://pypi.python.org/pypi/Pillow/2.0.0

like image 25
Adam Holloway Avatar answered Sep 21 '22 19:09

Adam Holloway