author | terom |
Wed, 30 Mar 2016 01:29:22 +0300 | |
changeset 153 | 47dbaa6b13b0 |
parent 140 | 6db2527b67cf |
permissions | -rw-r--r-- |
79 | 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 |
""" |
|
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 | 12 |
""" |
13 |
||
14 |
# the font we load |
|
15 |
font = None |
|
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 | 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) : |
|
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 | 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) |
|
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 | 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 |
|
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 | 72 |
|
73 |
# output buffer |
|
74 |
buf = StringIO() |
|
75 |
||
76 |
# save |
|
77 |
img.save(buf, 'png') |
|
78 |
||
79 |
# return data |
|
80 |
return buf.getvalue() |
|
81 |