tags:

views:

157

answers:

3

This image was created with PIL. See how the g's and the y's are cut off in this image? How can I prevent this?

The code that created this image is pretty straight forward (abbreviated):

import Image, ImageDraw, ImageFont

im = Image.new("RGBA", (200, 200), 'white')
draw = ImageDraw.Draw(im)

font = ImageFont.truetype("VeraSe.ttf", 12)

draw.text(
           (1, 1),
           " %s: " % "ggjyfFwe__",
           font=font,
           fill='black'
)

draw.text(
           (1, 30),
           " %s" % 15,
           font=font,
           fill='black'
)

im.show()

I tried it with a few different fonts, and it always gets clipped. Surprising;y, googleing "PIL font clipping" returns very few useful hits... I'm using python 2.6.4 and PIL 1.1.6 on Ubuntu 9.10

A: 

If you use PIL <=1.1.5, try 1.1.6 version, there was fixed very similarly sounded bug.

mykhal
hmm i am using 1.1.6
nbv4
A: 

My suggestion is, before you create the image object, to get the required size for the text.

This is done using font.getsize("text") (documentation).

In a image generating script I made, I first found the maximum height of one line of text, by calling the equvalient of font.getsize("Åj") (If you only need US-ASCII, you could find the height of "Aj" instead). Then I calculated the required image height and line offsets, including margins and line-spacing.

gnud
I already tried something like that. If the line height isn't enough, PIL will just overlap the text instead of clipping it...
nbv4
A: 

the font.getsize("Åj") trick works, but it would be better to use strings like "ÅAj" because some fonts don't have the "Å" and so this won't return what you would expect.

norbidur