[Image-SIG] Re: I have problem about PIL
Fredrik Lundh
fredrik@effbot.org
Thu, 3 May 2001 21:47:54 +0200
(nattiya, the mailing list software doesn't allow attachments over
40k or so, so your mail didn't get through to the list. however, I
managed to get my hands on it anyway...)
Nattiya wrote:
> I have used PIL, and I have problem with draw.text() module
> /.../ See the text that is in left. I want it to rotate 90 degree
> to stay along with Y-axis.
I've attached a simple font wrapper that lets you write "transposed"
text via the standard draw.text() function. to use this, create a font
in the usual way, and then wrap it
font = ImageFont.load(...)
vertical_font = TransposedFont(font, Image.ROTATE_90)
draw.text((x, y), text, vertical_font)
the second argument can be any valid argument to the Image object's
transpose method (i.e. ROTATE_90, ROTATE_180, ROTATE_270,
FLIP_LEFT_RIGHT, or FLIP_TOP_BOTTOM), or None to use the font
as is.
hope this helps!
Cheers /F
# TransposedFont.py
import Image
class TransposedFont:
def __init__(self, font, orientation=None):
self.font = font
self.orientation = orientation
def getsize(self, text):
w, h = self.font.getsize(text)
if self.orientation in (Image.ROTATE_90, Image.ROTATE_270):
return h, w
return w, h
def getmask(self, text):
im = self.font.getmask(text)
if self.orientation is not None:
return im.transpose(self.orientation)
return im
# end