aboutsummaryrefslogtreecommitdiffstats
path: root/python/label.py
blob: c154275cb087670af05654712887660fa61fa06e (plain) (blame)
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
from PIL import Image, ImageFont, ImageDraw

class label:
	def __init__(self, length, width=336, landscape=False):
		assert(width == 336 or width == 720)
		self.landscape = landscape
		self.fontfile = '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf'

		self.im = Image.new("1", (width, length), "white")
		if self.landscape:
			self.im = self.im.rotate(90)

	def set_font(self, fontfile):
		self.fontfile = fontfile

	def set_fontsize(self, size):
		self.font = ImageFont.truetype(self.fontfile, size)

	def text(self, pos, text):
		draw = ImageDraw.Draw(self.im)
		draw.text(pos, text, font=self.font)

	def save(self, outfile):
		if not self.landscape:
			self.im = self.im.rotate(90)
		self.im.save(outfile, "PNG")