[Image-SIG] text justification

Kevin Cazabon kevin at cazabon.com
Mon Nov 26 16:09:29 CET 2007

There's nothing built into the library, but I've hacked some code in  
the past to do this - unfortunately I can't find any code which can be  
neatly extracted for you.

It's pretty straight forward though - the PIL text rendering functions  
include the ability to tell you exactly what size the finished text  
will be.  You can simply use that, then calculate how far left/right  
to move your starting point.

i.e. - if your text area is 300 pixels wide, and the text will be only  
223 pixels, to right-justify you just offset by the difference (77  
pixels).  For center justified, just offset by half that (38 or 39  


On 26 Nov 2007, at 05:15, Scott Frankel wrote:

> I'm using PIL to render text.  Is there a way to specify left,
> center, or right justification?
> After setting X and Y coordinate positions, the text string, and the
> font, my text drawing line of code is this:
> 	draw.text((xpos, ypos), text, font=font)
> Thanks in advance
> Scott
> _______________________________________________
> Image-SIG maillist  -  Image-SIG at python.org
> http://mail.python.org/mailman/listinfo/image-sig

More information about the Image-SIG mailing list