log_formatter_pil.py
author Tero Marttila <terom@fixme.fi>
Thu, 12 Feb 2009 22:34:54 +0200
changeset 119 df859bfdd3be
parent 80 a0662cff1d9d
child 124 4bc4de14f006
permissions -rw-r--r--
add version string
"""
    Use of PIL to render the image formatting stuff
"""

from PIL import Image, ImageDraw, ImageFont

from cStringIO import StringIO

class PILImageFormatter (object) :
    """
        Mixin for LogFormatter that implements the basic image-rendering operations on top of format_txt
    """
    
    # the font we load
    font = None

    def _load_font (self) :
        """
            Use the configured img_ttf_path for a TrueType font, or a default one
        """

        if self.font :
            pass
        
        elif self.img_ttf_path :
            # load truetype with configured size
            self.font = ImageFont.truetype(self.img_ttf_path, self.img_font_size)

        else :
            # default
            self.font = ImageFont.load_default()

        return self.font

    def format_png (self, lines, **kwargs) :
        # load font
        font = self._load_font()

        # build list of plain-text line data
        lines = list(data for line, data in self.format_txt(lines, **kwargs))
        
        # lines sizes
        line_sizes = [font.getsize(line) for line in lines]

        # figure out how wide/high the image will be
        width = max(width for width, height in line_sizes)
        height = sum(height for width, height in line_sizes)

        # create new B/W image
        img = Image.new('L', (width, height), 0xff)

        # drawer
        draw = ImageDraw.Draw(img)
        
        # starting offset
        offset_y = 0

        # draw the lines
        for line, (width, height) in zip(lines, line_sizes) :
            # draw
            draw.text((0, offset_y), line, font=font)

            # next offset
            offset_y += height
        
        # output buffer
        buf = StringIO()

        # save
        img.save(buf, 'png')

        # return data
        return buf.getvalue()