=== modified file 'phatch/actions/text.py' --- phatch/actions/text.py 2009-06-11 16:21:43 +0000 +++ phatch/actions/text.py 2009-06-17 15:48:07 +0000 @@ -24,29 +24,57 @@ def init(): global Image, ImageDraw, ImageFont import Image, ImageDraw, ImageFont - -def draw_text(image, text, horizontal_offset, vertical_offset, size, - color='#FFFFFF',orientation=None, font=None): + +MIDDLE = _t('Middle') + +LEFT = _t('Left') +RIGHT = _t('Right') +HORIZONTAL_JUSTIFICATION = [LEFT, MIDDLE, RIGHT] + +TOP = _t('Top') +BOTTOM = _t('Bottom') +VERTICAL_JUSTIFICATION = [TOP, MIDDLE, BOTTOM] + +def draw_text(image, text, horizontal_offset, vertical_offset, + horizontal_justification, vertical_justification, size, + color='#FFFFFF',orientation=None, font=None): """Draws text on an image.""" - if orientation: - orientation = getattr(Image,orientation) + orientation = getattr(Image, orientation) if orientation else None + draw = ImageDraw.Draw(image) if font.strip(): font = ImageFont.truetype(font,size) else: font = ImageFont.load_default() text = text.encode('ascii','replace') + if orientation: font = ImageFont.TransposedFont(font,orientation) - #calculate offset - m_x, m_y = draw.textsize(text,font=font) - l_x, l_y = image.size - if horizontal_offset < 0: - horizontal_offset = l_x-m_x+horizontal_offset - if vertical_offset < 0: - vertical_offset = l_y-m_y+vertical_offset - #draw - draw.text((horizontal_offset,vertical_offset),text,font=font,fill=color) + + # check justifications + text_width, text_height = draw.textsize(text,font=font) + image_width, image_height = image.size + + if horizontal_justification == LEFT: + horizontal_location = 0 + if horizontal_justification == MIDDLE: + horizontal_location = (image_width - text_width) / 2 + if horizontal_justification == RIGHT: + horizontal_location = image_width - text_width + + if vertical_justification == TOP: + vertical_location = 0 + if vertical_justification == MIDDLE: + vertical_location = (image_height - text_height) / 2 + if vertical_justification == BOTTOM: + vertical_location = image_height - text_height + + # check offsets + horizontal_location += horizontal_offset + vertical_location += vertical_offset + + # draw + draw.text((horizontal_location, vertical_location), text, font=font, fill=color) # composite the watermark with the layer return image @@ -65,10 +93,12 @@ def interface(self,fields): fields[_t('Text')] = self.CharField(choices=self.STAMPS) - fields[_t('Horizontal Offset')] = self.PixelField('5%', - choices=self.SMALL_PIXELS) - fields[_t('Vertical Offset')] = self.PixelField('-5%', - choices=self.SMALL_PIXELS) + fields[_t('Horizontal Offset')] = self.PixelField('0%', choices=self.SMALL_PIXELS) + fields[_t('Vertical Offset')] = self.PixelField('0%', choices=self.SMALL_PIXELS) + fields[_t('Horizontal Justification')] = self.ChoiceField(HORIZONTAL_JUSTIFICATION[1], + HORIZONTAL_JUSTIFICATION) + fields[_t('Vertical Justification')] = self.ChoiceField(VERTICAL_JUSTIFICATION[1], + VERTICAL_JUSTIFICATION) fields[_t('Font')] = self.FontFileField('Free Sans') fields[_t('Size')] = self.PixelField('5%', choices=self.SMALL_PIXELS) @@ -81,7 +111,7 @@ x, y = info[new('Pil','Size')] return super(Action,self).values(info, pixel_fields={ - 'Horizontal Offset' : x, + 'Horizontal Offset' : x, 'Vertical Offset' : y, 'Size' : (x+y)/2, })