Skip to content

Instantly share code, notes, and snippets.

@Krewn
Created June 12, 2016 23:04
Show Gist options
  • Save Krewn/b5720e456552a87941076de722354ae7 to your computer and use it in GitHub Desktop.
Save Krewn/b5720e456552a87941076de722354ae7 to your computer and use it in GitHub Desktop.
In case you are allergic to bitmaps...
import Image, ImageDraw
#Characters are fixed font drawn on a 2x4 grid where y = 3 is the text line bottm.
characters = {'1':[[(0,1),(1,0),(1,3)],[(0,3),(2,3)]],'2':[[(0,1),(1,0),(2,1),(0,3),(2,3)]],'3':[[(0,1),(1,0),(2,1),(1,1.5),(2,2),(1,3),(0,2)]],'4':[[(0,0),(0,1.5),(2,1.5)],[(2,0),(2,3)]],'5':[[(2,0),(0,0),(0,1,),(1,1),(2,1.75),(2,2.25),(1,3),(0,3)]],'6':[[(1,0),(0,1),(0,3),(2,3),(2,2),(0,2)]],'7':[[(0,0),(2,0),(1,3)]],'8':[[(0,0),(2,0),(2,1),(1.5,1.5),(2,2.5),(2,3),(0,3),(0,2),(0.5,1.5),(0,0.5),(0,0)],[(0.5,1.5),(1.5,1.5)]],'9':[[(0,3),(2,2),(2,0),(0,0),(0,1),(2,1)]],'0':[[(0,0),(0,3),(2,3),(2,0),(0,0),(2,3)]],'a':[[(1.5,3),(1.5,1),(0,1),(0,3),(1.5,3),(2,3)]],'b':[[(0,0),(0,3),(2,3),(2,2),(0,2)]],'c':[[(2,1),(1,1),(0,1.5),(0,2.5),(1,3),(2,3)]],'d':[[(2,0),(2,3),(0,3),(0,2),(2,2)]],'e':[[(2,3),(0,3),(0,1),(2,1),(2,2),(0,2)]],'f':[[(1,3),(1,0),(2,0)],[(0,1.5),(2,1.5)]],'g':[[(2,3),(0,3),(0,1),(2,1),(2,4),(1,4)]],'h':[[(0,3),(0,0)],[(0,2),(2,2),(2,3)]],'i':[[(1,3),(1,1.5)],[(0.75,1.25),(1.25,0.75)]],'j':[[(0,4),(1,4),(1,1.5)],[(0.75,1.25),(1.25,0.75)]],'k':[[(0,3),(0,0)],[(0,2),(1,1)],[(0,2),(2,3)]],'l':[[(1,3),(1,0)]],'m':[[(0,3),(0,1),(2,1),(2,3)],[(1,1),(1,3)]],'n':[[(0,3),(0,1),(2,1),(2,3)]],'o':[[(0,1),(2,1),(2,3),(0,3),(0,1)]],'p':[[(0,4),(0,1),(2,1),(2,3),(0,3)]],'q':[[(2,4),(2,1),(0,1),(0,3),(2,3)]],'r':[[(0,3),(0,1)],[(0,1.5),(1,1),(2,1)]],'s':[[(2,1),(0,1),(0,2),(2,2),(2,3),(0,3),]],'t':[[(1,0),(1,3)],[(0,1.5),(2,1.5)]],'u':[[(0,1),(0,3),(2,3),(2,1)]],'v':[[(0,1),(1,3),(2,1)]],'w':[[(0,1),(0,3),(1,2),(2,3),(2,1)]],'x':[[(0,1),(2,3)],[(0,3),(2,1)]],'y':[[(0,1),(0,3),(2,3)],[(2,1),(2,4),(1,4)]],'z':[[(0,1),(2,1),(0,3),(2,3)]],'A':[[(0,3),(1,0),(2,3)],[(0,1.5),(2,1.5)]],'B':[[(0,3),(0,0),(1.5,0),(1.5,1.5)],[(0,3),(2,3),(2,1.5),(0,1.5)]],'C':[[(2,0),(0,0),(0,3),(2,3)]],'D':[[(0,0),(0,3),(1.5,3),(2,2.5),(2,0.5),(1.5,0),(0,0)]],'E':[[(2,0),(0,0),(0,3),(2,3)],[(0,1.5),(1,1.5)]],'F':[[(0,3),(0,0),(2,0)],[(0,1.5),(1,1.5)]],'G':[[(2,0.5),(2,0),(0,0),(0,3),(2,3),(2,1.5),(1,1.5)]],'H':[[(0,0),(0,3)],[(0,1.5),(2,1.5)],[(2,0),(2,3)]],'I':[[(0,0),(2,0)],[(1,0),(1,3)],[(0,3),(2,3)]],'J':[[(0,0),(2,0)],[(1,0),(1,3),(0,2)]],'K':[[(0,0),(0,3)],[(2,0),(0,1.5),(2,3)]],'L':[[(0,0),(0,3),(2,3)]],'M':[[(0,3),(0,0),(1,1),(2,0),(2,3)]],'N':[[(0,3),(0,0),(2,3),(2,0)]],'O':[[(0,0),(0,3),(2,3),(2,0),(0,0)]],'P':[[(0,3),(0,0),(2,0),(2,1.5),(0,1.5)]],'Q':[[(2,3),(2,0),(0,0),(0,3),(2,3),(1,2)]],'R':[[(0,3),(0,0),(2,0),(2,1),(0,1.5),(2,3)]],'S':[[(2,0),(0,0),(0,1),(2,2),(2,3),(0,3)]],'T':[[(0,0),(2,0)],[(1,3),(1,0)]],'U':[[(0,0),(0,3),(2,3),(2,0)]],'V':[[(0,0),(1,3),(2,0)]],'W':[[(0,0),(0,3),(1,2),(2,3),(2,0)]],'X':[[(0,0),(2,3)],[(0,3),(2,0)]],'Y':[[(0,0),(1,1),(2,0)],[(1,1),(1,3)]],'Z':[[(0,0),(2,0),(0,3),(2,3)]]}
def tupleSum(a,b):
return tuple([sum(k) for k in zip(a,b)])
def scale(vector,scaler):
return tuple([scaler*k for k in vector])
def DrawLine(start, end, img, relativeLocation = (0,0) ,size = 1):
draw = ImageDraw.Draw(img)
draw.line( tupleSum(relativeLocation,scale(start,size))+tupleSum(relativeLocation,scale(end,size)), fill=128)
del draw
def drawLetter(letter,scale,img,relativeLocation,size = 1):
global characters
try:
lines = characters[letter]
except IndexError:
print letter+" is not supported: to add suport for this letter make an entery in the characters variable."
for k in lines:
for k2 in range(1,len(k)):
DrawLine(k[k2-1], k[k2], img, relativeLocation, size)
def drawText(text,img,relativeLocation,size = 1):
step = size*3
place = list(relativeLocation)
for k in text:
drawLetter(k, scale, img, place, size)
place[0] += step
def testFont():
im = Image.new("RGB", (900, 512), "white")
numbers = "1234567890"
lowerCase = "abcdefghijklmnopqrstuvwxyz"
upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
drawText(upperCase,im,(5,5),3)
drawText(lowerCase,im,(5,50),3)
drawText(numbers,im,(5,100),3)
im.show()
#testFont()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment