tags:

views:

136

answers:

1

hi I have a few stings I would like to draw to imagea. the thing is that I want each string to be displayed in the middle of each image. how do I do it? or how can I know the length in pixels of a string (giving ofcourse the font)? thanks

A: 

PIL font's do have a getsize method which you can use to get width and height of font, did you try that? e.g.

from PIL import Image, ImageDraw, ImageFont

fontFile = "/usr/share/fonts/truetype/freefont/FreeSansBold.ttf"
font = ImageFont.truetype(fontFile, 10)
text="anurag"
print font.getsize(text)
assert font.getsize(text)[0]*2 == font.getsize(text*2)[0]
Anurag Uniyal