python - PIL ImageDraw.textsize returns incorrect height -
i'm having issue pil's imagedraw module, draw.textsize
method. method supposed take string , font, , return width , height string occupy when rendered in font. seems have lower bound on height returns, though: can't convince return lower 43
. here's example (link) show i'm looking @ (bounding boxes drawn around text based on returned width & height), , here's code produced it:
from pil import image, imagedraw, imagefont # pil 1.1.7; python 2.6.6 im = image.open(r'c:\test\blank.png').convert('rgb') draw = imagedraw.draw(im) textcolor = (0, 0, 0) x = 10 y = 3 fontsize in xrange(8, 51): # other fonts behave same way font = imagefont.truetype('arial.ttf', fontsize) text = 'hello, world! size %d' % fontsize width, height = draw.textsize(text, font=font) print 'font size %d: %d x %d' % (fontsize, width, height) bbox = [(x, y), (x+width, y+height)] draw.rectangle(bbox, outline=textcolor) draw.text((x, y), text, font=font, fill=textcolor) y += height + 3 im.show()
once font gets size 38, bounding box stretches match correctly, before that, it's set static 43
. question is, know why imagedraw behaving way, , know of way fix it? i'm working around issue setting:
width = min(width, fontsize+1)
...but that's not robust solution ever devised.
the basic issue seems pil super buggy , no longer supported. problem mentioned here isn't worst of (e.g. no 1 able replicate because it's hard install...).
in light of troubles seem rampant in pil 1.1.7, best solution seems to install pillow , move along. doesn't require code changes code runs pil (it's pil fork installs "pil" library), , seems vastly friendlier (and still active). commenters on question have confirmed, it's simple, trouble-free install , works it's supposed to.
Comments
Post a Comment