-
Notifications
You must be signed in to change notification settings - Fork 1
/
write_ppm.py
62 lines (53 loc) · 1.72 KB
/
write_ppm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from PIL import Image,ImageFont,ImageDraw
import os
_height=16
_width=32
_fonts=['/usr/local/texlive/2011/texmf-dist/fonts/truetype/public/gnu-freefont/FreeSans.ttf',
'/usr/share/fonts/truetype/freefont/FreeSans.ttf']
for _font in _fonts:
if os.path.exists(_font):
break
if not os.path.exists(_font):
print 'No font file found'
_font=None
_fontsize=12
class textppm():
def __init__(self, text,
color=(255,255,255),
grey=None,
font=_font,
fontsize=_fontsize,
height=_height,
width=_width):
if len(color)==1:
self.color=(color,color,color)
else:
self.color=color
self.grey=grey
self.fontsize=_fontsize
try:
self.font=ImageFont.truetype(_font,
self.fontsize)
except:
self.font=ImageFont.load_default()
if len(text)>0:
self.drawtext(text)
else:
self.text=None
self.im=None
self.drawtext=None
self.width=None
def drawtext(self, text):
self.text=text
self.im = Image.new("RGB", (_width, _height))
self.draw = ImageDraw.Draw(self.im)
self.width=self.font.getsize(self.text)[0]
self.im = Image.new("RGB", (self.width, _height))
self.draw = ImageDraw.Draw(self.im)
if self.grey is not None:
self.color=tuple([int(x*self.grey) for x in self.color])
self.draw.text((0, 0),self.text,
self.color,
font=self.font)
def write(self, filename):
self.im.save(filename)