log_formatter_pil.py
changeset 140 6db2527b67cf
parent 139 9c7769850195
child 141 65c98c9e1716
equal deleted inserted replaced
139:9c7769850195 140:6db2527b67cf
     1 """
       
     2     Use of PIL to render the image formatting stuff
       
     3 """
       
     4 
       
     5 from PIL import Image, ImageDraw, ImageFont
       
     6 
       
     7 from cStringIO import StringIO
       
     8 
       
     9 class PILImageFormatter (object) :
       
    10     """
       
    11         Mixin for LogFormatter that implements the basic image-rendering operations on top of format_txt
       
    12     """
       
    13     
       
    14     # the font we load
       
    15     font = None
       
    16 
       
    17     # line spacing in pixels
       
    18     LINE_SPACING = 1
       
    19 
       
    20     def _load_font (self) :
       
    21         """
       
    22             Use the configured img_ttf_path for a TrueType font, or a default one
       
    23         """
       
    24 
       
    25         if self.font :
       
    26             pass
       
    27         
       
    28         elif self.img_ttf_path :
       
    29             # load truetype with configured size
       
    30             self.font = ImageFont.truetype(self.img_ttf_path, self.img_font_size)
       
    31 
       
    32         else :
       
    33             # default
       
    34             self.font = ImageFont.load_default()
       
    35 
       
    36         return self.font
       
    37 
       
    38     def format_png (self, lines, **kwargs) :
       
    39         """
       
    40             Build and return a PNG image of the given lines, using format_txt
       
    41         """
       
    42 
       
    43         # load font
       
    44         font = self._load_font()
       
    45 
       
    46         # build list of plain-text line data
       
    47         lines = list(data for line, data in self.format_txt(lines, **kwargs))
       
    48         
       
    49         # lines sizes
       
    50         line_sizes = [font.getsize(line) for line in lines]
       
    51 
       
    52         # figure out how wide/high the image will be
       
    53         width = max(width for width, height in line_sizes)
       
    54         height = sum(height + self.LINE_SPACING for width, height in line_sizes)
       
    55 
       
    56         # create new B/W image
       
    57         img = Image.new('L', (width, height), 0xff)
       
    58 
       
    59         # drawer
       
    60         draw = ImageDraw.Draw(img)
       
    61         
       
    62         # starting offset
       
    63         offset_y = 0
       
    64 
       
    65         # draw the lines
       
    66         for line, (width, height) in zip(lines, line_sizes) :
       
    67             # draw
       
    68             draw.text((0, offset_y), line, font=font)
       
    69 
       
    70             # next offset
       
    71             offset_y += height + self.LINE_SPACING
       
    72         
       
    73         # output buffer
       
    74         buf = StringIO()
       
    75 
       
    76         # save
       
    77         img.save(buf, 'png')
       
    78 
       
    79         # return data
       
    80         return buf.getvalue()
       
    81