log_formatter_pil.py
author Tero Marttila <terom@fixme.fi>
Wed, 11 Feb 2009 03:32:21 +0200
changeset 98 8c6e36849f9a
parent 80 a0662cff1d9d
child 124 4bc4de14f006
permissions -rw-r--r--
implement tempfile use for scripts/search-index autoload, so that it can resume aborted sessions
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Use of PIL to render the image formatting stuff
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
from PIL import Image, ImageDraw, ImageFont
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
from cStringIO import StringIO
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
class PILImageFormatter (object) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    """
80
a0662cff1d9d add RSSFormatter, and link to different types from channel_last.tmpl...
Tero Marttila <terom@fixme.fi>
parents: 79
diff changeset
    11
        Mixin for LogFormatter that implements the basic image-rendering operations on top of format_txt
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    # the font we load
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    font = None
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    def _load_font (self) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
            Use the configured img_ttf_path for a TrueType font, or a default one
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
        """
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
        if self.font :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
            pass
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
        
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
        elif self.img_ttf_path :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
            # load truetype with configured size
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
            self.font = ImageFont.truetype(self.img_ttf_path, self.img_font_size)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
        else :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
            # default
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
            self.font = ImageFont.load_default()
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
        return self.font
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
    def format_png (self, lines, **kwargs) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
        # load font
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
        font = self._load_font()
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
        # build list of plain-text line data
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
        lines = list(data for line, data in self.format_txt(lines, **kwargs))
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
        
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
        # lines sizes
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
        line_sizes = [font.getsize(line) for line in lines]
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
        # figure out how wide/high the image will be
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
        width = max(width for width, height in line_sizes)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        height = sum(height for width, height in line_sizes)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
        # create new B/W image
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        img = Image.new('L', (width, height), 0xff)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
        # drawer
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
        draw = ImageDraw.Draw(img)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
        
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
        # starting offset
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
        offset_y = 0
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
        # draw the lines
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
        for line, (width, height) in zip(lines, line_sizes) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
            # draw
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
            draw.text((0, offset_y), line, font=font)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
            # next offset
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
            offset_y += height
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
        
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        # output buffer
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
        buf = StringIO()
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
        # save
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
        img.save(buf, 'png')
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
        # return data
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
        return buf.getvalue()
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74