Permalink
Browse files

clock converted to font/image

  • Loading branch information...
1 parent c7fb5b7 commit 44181bf0a05bb59ddf88f6c9b98e359eea6bad25 @phooky phooky committed May 6, 2013
Showing with 58 additions and 1 deletion.
  1. +51 −0 hexaservice/fontutil.py
  2. +7 −1 hexaservice/service.py
View
@@ -0,0 +1,51 @@
+#!/usr/src/python
+
+from PIL import Image, ImageDraw, ImageChops
+
+
+def getChar(img,x):
+ """Extract the character starting at x in img.
+ Returns a tuple (image, next x after the marker).
+ Uses a pixel of value (255,0,0) as the marker."""
+ nextx = x
+ while img.getpixel((nextx,0)) != (255,0,0):
+ nextx = nextx + 1
+ charimg = img.crop((x,0,nextx,7))
+ return (charimg, nextx + 1)
+
+class Font:
+ def __init__(self,path,inventory):
+ x = 0
+ self.fontmap = {}
+ self.baseimg = Image.open(path)
+ for c in inventory:
+ (charimg,x) = getChar(self.baseimg,x)
+ self.fontmap[c] = ImageChops.invert(charimg)
+ def strWidth(self, chars):
+ w = 0
+ for c in chars:
+ try:
+ w = w + self.fontmap[c].size[0]
+ except KeyError:
+ w = w + 2
+ w = w + len(chars)-1
+ return w
+
+ def strImg(self,chars):
+ img = Image.new("1",(self.strWidth(chars),7))
+ #d = ImageDraw.Draw(img)
+ #d.rectangle(((0,0),img.size),fill=255)
+ x = 0
+ for c in chars:
+ try:
+ img.paste(self.fontmap[c],(x,0))
+ x = x + self.fontmap[c].size[0]
+ except KeyError:
+ x = x + 2
+ x = x + 1
+ return img
+
+base_font = Font('basic-font.png',"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.!?@/:;()#abcdefghijklmnopqrstuvwxyz,=^|-_+'\"")
+
+if __name__ == '__main__':
+ base_font.strImg('hello world').show()
View
@@ -1,6 +1,8 @@
#!/usr/bin/python
import led_panel
from led_panel import panels
+from font_util import basic_font
+from PIL import Image
import time
import signal
import sys
@@ -20,7 +22,11 @@ def sigint_handler(signal,frame):
while True:
msg = time.strftime("%H:%M:%S")
for j in range(3):
- panels[j].setMessage(msg,0,0)
+ txtimg = base_font.strImg(msg)
+ img = Image.new("1",(120,7))
+ img.paste(txtimg,(0,0))
+ img.paste(txtimg,(60,0))
+ panels[j].setImage(img,0,0)
time.sleep(0.15)
panels[0].setRelay(False)

0 comments on commit 44181bf

Please sign in to comment.