[Image-SIG] Interesting Bounding box calculation

Ned Batchelder ned at nedbatchelder.com
Tue Jan 16 19:27:05 CET 2007

I'm not sure exactly what you are showing me here.  Perhaps you could 
describe what you see (or even post the images).  Prefixing the string 
with spaces will of course prevent the problem, but introduces problems 
of its own (how to accurately position the text, how to avoid the spaces 
clobbering other text, etc).  You're using font.getmetrics() here, which 
is an undocumented function.  Reading the code, it seems to return the 
ascent and descent of the font, so using either dimension for an x value 
seems wrong to me, and I'm not sure why you are using it in the first place.

I want to understand.  Can you explain?


Scott David Daniels wrote:
> Ned Batchelder wrote:
>> OK, here's some code, and some output....
> After looking, and doing a bit of experimenting, here is a
> surprising drawing:
>      import Image, ImageDraw, ImageFont
>      def paints(fontname='timesbi.ttf', fontsize=50,
>                canvas=(500, 700), position=(100, 100)):
>          im = Image.new("RGB", canvas, '#ffffc0')
>          draw = ImageDraw.Draw(im)
>          font = ImageFont.truetype(fontname, fontsize)
>          fh, fx = font.getmetrics()
>          x, y = position
>          for text in ['j_0_|igf', ' j_1_|igf', '  j_2_|igf', 'young']:
> 	    width, height = font.getsize(text) # w,h
>              draw.rectangle((x, y, x+width, y+height),
>                             fill='lightgrey', outline='lightgreen')
>              draw.rectangle((x-fx, y, x, y+fh),
>                             fill='lightgrey', outline='red')
>              draw.text((x, y), text, font=font, fill='black')
>              y += 100
>          return im
>      paints().show()
> -- Scott David Daniels
> Scott.Daniels at Acm.Org
> _______________________________________________
> Image-SIG maillist  -  Image-SIG at python.org
> http://mail.python.org/mailman/listinfo/image-sig

Ned Batchelder, http://nedbatchelder.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/image-sig/attachments/20070116/a7206c7e/attachment.htm 

More information about the Image-SIG mailing list