[Image-SIG] Interesting Bounding box calculation

Scott David Daniels Scott.Daniels at Acm.Org
Tue Jan 16 16:47:57 CET 2007


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



More information about the Image-SIG mailing list