log_formatter_pil.py
changeset 124 4bc4de14f006
parent 80 a0662cff1d9d
equal deleted inserted replaced
123:3297596ab606 124:4bc4de14f006
    11         Mixin for LogFormatter that implements the basic image-rendering operations on top of format_txt
    11         Mixin for LogFormatter that implements the basic image-rendering operations on top of format_txt
    12     """
    12     """
    13     
    13     
    14     # the font we load
    14     # the font we load
    15     font = None
    15     font = None
       
    16 
       
    17     # line spacing in pixels
       
    18     LINE_SPACING = 1
    16 
    19 
    17     def _load_font (self) :
    20     def _load_font (self) :
    18         """
    21         """
    19             Use the configured img_ttf_path for a TrueType font, or a default one
    22             Use the configured img_ttf_path for a TrueType font, or a default one
    20         """
    23         """
    31             self.font = ImageFont.load_default()
    34             self.font = ImageFont.load_default()
    32 
    35 
    33         return self.font
    36         return self.font
    34 
    37 
    35     def format_png (self, lines, **kwargs) :
    38     def format_png (self, lines, **kwargs) :
       
    39         """
       
    40             Build and return a PNG image of the given lines, using format_txt
       
    41         """
       
    42 
    36         # load font
    43         # load font
    37         font = self._load_font()
    44         font = self._load_font()
    38 
    45 
    39         # build list of plain-text line data
    46         # build list of plain-text line data
    40         lines = list(data for line, data in self.format_txt(lines, **kwargs))
    47         lines = list(data for line, data in self.format_txt(lines, **kwargs))
    42         # lines sizes
    49         # lines sizes
    43         line_sizes = [font.getsize(line) for line in lines]
    50         line_sizes = [font.getsize(line) for line in lines]
    44 
    51 
    45         # figure out how wide/high the image will be
    52         # figure out how wide/high the image will be
    46         width = max(width for width, height in line_sizes)
    53         width = max(width for width, height in line_sizes)
    47         height = sum(height for width, height in line_sizes)
    54         height = sum(height + self.LINE_SPACING for width, height in line_sizes)
    48 
    55 
    49         # create new B/W image
    56         # create new B/W image
    50         img = Image.new('L', (width, height), 0xff)
    57         img = Image.new('L', (width, height), 0xff)
    51 
    58 
    52         # drawer
    59         # drawer
    59         for line, (width, height) in zip(lines, line_sizes) :
    66         for line, (width, height) in zip(lines, line_sizes) :
    60             # draw
    67             # draw
    61             draw.text((0, offset_y), line, font=font)
    68             draw.text((0, offset_y), line, font=font)
    62 
    69 
    63             # next offset
    70             # next offset
    64             offset_y += height
    71             offset_y += height + self.LINE_SPACING
    65         
    72         
    66         # output buffer
    73         # output buffer
    67         buf = StringIO()
    74         buf = StringIO()
    68 
    75 
    69         # save
    76         # save