[Image-SIG] TrueType/OpenType rendering bug in PIL 1.1.7?

Anders Sandvig anders.sandvig at gmail.com
Thu Oct 7 00:01:14 CEST 2010


I ran the following code in Python 2.6.1 with PIL 1.1.7 on Mac OS X 10.6.4:

#!/usr/bin/python
from PIL import Image, ImageDraw, ImageFont

image = Image.new("RGB", (800, 600), (127, 127, 127))
drawer = ImageDraw.Draw(image)

fonts = ("Verdana Bold.ttf", "Verdana Bold Italic.ttf",
         "Arial Bold.ttf", "Arial Bold Italic.ttf",
         "AGaramondPro-Bold.otf", "AGaramondPro-BoldItalic.otf",
         "Zapfino.ttf")

x = 100
y = 20
for name in fonts:
    font = ImageFont.truetype("/Library/Fonts/" + name, 32)
    text = "fish & chips (%s)" % (name)
    (w, h) = font.getsize(text)
    drawer.rectangle([x, y, x + w, y + h], fill=(0, 170, 0))
    drawer.text((x, y), text, font=font, fill=(255, 255, 255))
    y += h + 20

image.save("fontbug.png", "PNG")

The result can be found here:
http://files.looplabel.net/pil/fontbug.png

As you can see, the last two fonts are rendered outside the expected
box (as returned by ImageFont.getsize()).

It's also odd how the ending first 'f' of the Garamond Bold Italic
rendering is clipped to the left (compared to the second 'f' in the
sentence), as is the ending parenthesis of the Arial Bold Italic
rendering (clipped on the right, seemingly on the edge of the box
calculated by ImageFont.getsize()).

I am not sure which version of FreeType2 PIL is compiled against (PIL
was compiled from source), but it's the version installed in
/usr/X11/lib and /usr/X11/include on my system (i.e. whatever came
with Mac OS X, I haven't installed any custom versions).

I may be wrong, but as a naive user, I would expect ImageDraw.text()
to draw inside the same area as returned by ImageFont.getsize(). I've
also tried substituting ImageFont.getsize() for ImageDraw.textsize(),
but the results are the same.

So anyone have any clue on what's causing this and/or how to fix it?

... or maybe I'm just using the functions the wrong way?

BTW: I get the same (unexpected) result when running on a Windows
XP/Cygwin system (same versions of Python and PIL), compiled against
whatever is the latest version of FreeType2 for Cygwin -- I can check
when I access the Windows machine tomorrow.


Regards,

Anders


More information about the Image-SIG mailing list