log_formatter_pil.py
author Tero Marttila <terom@fixme.fi>
Mon, 16 Feb 2009 00:54:25 +0200
changeset 132 0e857c4a67de
parent 124 4bc4de14f006
permissions -rw-r--r--
cache version across calls to version_mercurial, so as to avoid opening the repo every time
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
124
4bc4de14f006 remove support for default font, as it doesn't support unicode, and implement a 1px line spacing for format_png
Tero Marttila <terom@fixme.fi>
parents: 80
diff changeset
    17
    # line spacing in pixels
4bc4de14f006 remove support for default font, as it doesn't support unicode, and implement a 1px line spacing for format_png
Tero Marttila <terom@fixme.fi>
parents: 80
diff changeset
    18
    LINE_SPACING = 1
4bc4de14f006 remove support for default font, as it doesn't support unicode, and implement a 1px line spacing for format_png
Tero Marttila <terom@fixme.fi>
parents: 80
diff changeset
    19
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
    def _load_font (self) :
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
            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
    23
        """
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
        if self.font :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
            pass
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
        
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
        elif self.img_ttf_path :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
            # load truetype with configured size
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
            self.font = ImageFont.truetype(self.img_ttf_path, self.img_font_size)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
        else :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
            # default
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
            self.font = ImageFont.load_default()
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
        return self.font
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
    def format_png (self, lines, **kwargs) :
124
4bc4de14f006 remove support for default font, as it doesn't support unicode, and implement a 1px line spacing for format_png
Tero Marttila <terom@fixme.fi>
parents: 80
diff changeset
    39
        """
4bc4de14f006 remove support for default font, as it doesn't support unicode, and implement a 1px line spacing for format_png
Tero Marttila <terom@fixme.fi>
parents: 80
diff changeset
    40
            Build and return a PNG image of the given lines, using format_txt
4bc4de14f006 remove support for default font, as it doesn't support unicode, and implement a 1px line spacing for format_png
Tero Marttila <terom@fixme.fi>
parents: 80
diff changeset
    41
        """
4bc4de14f006 remove support for default font, as it doesn't support unicode, and implement a 1px line spacing for format_png
Tero Marttila <terom@fixme.fi>
parents: 80
diff changeset
    42
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
        # load font
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
        font = self._load_font()
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
        # build list of plain-text line data
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        lines = list(data for line, data in self.format_txt(lines, **kwargs))
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
        # lines sizes
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        line_sizes = [font.getsize(line) for line in lines]
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
        # figure out how wide/high the image will be
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
        width = max(width for width, height in line_sizes)
124
4bc4de14f006 remove support for default font, as it doesn't support unicode, and implement a 1px line spacing for format_png
Tero Marttila <terom@fixme.fi>
parents: 80
diff changeset
    54
        height = sum(height + self.LINE_SPACING for width, height in line_sizes)
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
        # create new B/W image
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
        img = Image.new('L', (width, height), 0xff)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
        # drawer
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
        draw = ImageDraw.Draw(img)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
        
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
        # starting offset
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
        offset_y = 0
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
        # draw the lines
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        for line, (width, height) in zip(lines, line_sizes) :
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
            # draw
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
            draw.text((0, offset_y), line, font=font)
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
            # next offset
124
4bc4de14f006 remove support for default font, as it doesn't support unicode, and implement a 1px line spacing for format_png
Tero Marttila <terom@fixme.fi>
parents: 80
diff changeset
    71
            offset_y += height + self.LINE_SPACING
79
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
        
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
        # output buffer
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
        buf = StringIO()
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
        # save
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
        img.save(buf, 'png')
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
        # return data
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
        return buf.getvalue()
43ac75054d5c image formatting \o/
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81