TrueType/OpenType rendering bug in PIL 1.1.7?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

TrueType/OpenType rendering bug in PIL 1.1.7?

Anders Sandvig
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
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: TrueType/OpenType rendering bug in PIL 1.1.7?

Edward Cannon
I ran you code on my machine (win 7 Python 2.6.4, PIL 1.1.6) and after
changing the font names (they are different on my system) I got
similar problems, but not the same. I did notice that your image has
"fish & fhips" whereas mine says "fish & chips" Something seems to be
slightly off.
Hope that helps.
Edward Cannon

On Wed, Oct 6, 2010 at 3:01 PM, Anders Sandvig <[hidden email]> wrote:

> 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
> _______________________________________________
> Image-SIG maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/image-sig
>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: TrueType/OpenType rendering bug in PIL 1.1.7?

Russell Owen
In reply to this post by Anders Sandvig
In article
<AANLkTikmfd59395T0WVNDHQ-AQrVeJ=[hidden email]>,
 Anders Sandvig <[hidden email]> wrote:

Not much help, I fear, but I tried it on my system and I don't have the
fonts that were causing you the problem. The results on the fonts I have
look about like yours.

I created a binary installer for PIL 1.1.7 that is built against
freetype 2.3.12 (which I believe is the latest) here:
<http://www.astro.washington.edu/users/rowen/python/>
but I used all the default build options for freetype; it has a lot of
options to configure the build and you may wish to look into them.

-- Russell

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: TrueType/OpenType rendering bug in PIL 1.1.7?

Anders Sandvig
In reply to this post by Edward Cannon
On Thu, Oct 7, 2010 at 9:05 PM, Edward Cannon <[hidden email]> wrote:
> I ran you code on my machine (win 7 Python 2.6.4, PIL 1.1.6) and after
> changing the font names (they are different on my system) I got
> similar problems, but not the same. I did notice that your image has
> "fish & fhips" whereas mine says "fish & chips" Something seems to be
> slightly off.

Sorry about that. I changed the 'c' to an 'f' in the last minute
before I rendered the image to illustrate the cut-off of the first 'f'
(the second 'f' is for comparison). I forgot to update the source code
in the e-mail...


Anders
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: TrueType/OpenType rendering bug in PIL 1.1.7?

Anders Sandvig
In reply to this post by Russell Owen
On Thu, Oct 7, 2010 at 9:26 PM, Russell E. Owen <[hidden email]> wrote:
> I created a binary installer for PIL 1.1.7 that is built against
> freetype 2.3.12 (which I believe is the latest) here:
> <http://www.astro.washington.edu/users/rowen/python/>
> but I used all the default build options for freetype; it has a lot of
> options to configure the build and you may wish to look into them.

I see. I tried installing FreeType2 via fink, but couldn't get it to
work. Fink says the install was successful, but I can't find the files
in /sw/lib or /sw/include (and neither can PIL's setup.py). I think a
useful first step would be to find out if the issue is caused by
FreeType2 or PIL, so building FreeType2 from source and tweaking some
options might help with that.

I have also noticed a similar problem when drawing italic or "skewed"
fonts in PhotoShop. When I select the text for editing, the text
renders outside the selection box, just like in my PIL example, so
maybe this is a known issue with TrueType/OpenType fonts?

I'm not sure if the odd rendering affects centering in PS or if it's
just a visual quirk when editing, but in my code it's causing problems
because I want to use the width returned from getsize() to center the
text.


Anders
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig