-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextBuilder.py
executable file
·160 lines (143 loc) · 5.81 KB
/
TextBuilder.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
''' TextBuilder.py - Class for building text images & scrolling them on the Ledgrid.'''
import sys
from Ledgrid import *
from PIL import Image
from random import randrange
# ================================================================================
class TextBuilder(SpriteSheet):
color = "random"
letter_coords = {
'frogger_F' : (16, 368),
'frogger_R' : (40, 368),
'frogger_O' : (64, 368),
'frogger_G' : (88, 368),
'frogger_E' : (112, 368),
'frogger_ ' : (136, 368), # space
'0' : (204, 28),
'1' : (220, 28),
'2' : (236, 28),
'3' : (252, 28),
'4' : (268, 28),
'5' : (284, 28),
'6' : (300, 28),
'7' : (316, 28),
'8' : (332, 28),
'9' : (348, 28),
'A' : (204, 44),
'B' : (220, 44),
'C' : (236, 44),
'D' : (252, 44),
'E' : (268, 44),
'F' : (284, 44),
'G' : (300, 44),
'H' : (316, 44),
'I' : (332, 44),
'J' : (348, 44),
'K' : (204, 60),
'L' : (220, 60),
'M' : (236, 60),
'N' : (252, 60),
'O' : (268, 60),
'P' : (284, 60),
'Q' : (300, 60),
'R' : (316, 60),
'S' : (332, 60),
'T' : (348, 60),
'U' : (204, 76),
'V' : (220, 76),
'W' : (236, 76),
'X' : (252, 76),
'Y' : (268, 76),
'Z' : (284, 76),
'-' : (300, 76),
' ' : (316, 76),
'Copyright' : (332, 76),
'Box' : (348, 76),
}
def __init__( self ):
h = SpriteSheet.__init__( self, 'sprite_sheets/Frogger.png', stepx=16, stepy=16, offsetx=32, offsety=32 )
return( h )
def setColor( self, newcolor ):
self.color = newcolor
def createStringImage( self, mystr, printinfo=True ):
sizex = 8
sizey = 16
space_sizex = 4 # make space characters smaller
img1 = Image.open( "./sprite_sheets/frogger.png" )
ni_size_x = (len(mystr)*sizex) - (mystr.count(' ') * (8-space_sizex)) # make new image size take into account the smaller size of spaces
ni = Image.new( 'RGB', (ni_size_x, sizey) )
paste_loc_x = 0
paste_loc_y = 0
space_letter_coords = self.letter_coords.get(' ')
for idx, character in enumerate( mystr ):
key = character
(x1, y1) = self.letter_coords.get( key, space_letter_coords ) # get the coords asked for if they exist, else return coords for a space
if( self.color == 'white' ):
pass
elif( self.color == 'yellow' ):
y1 += 1 * 64
elif( self.color == 'red' ):
y1 += 2 * 64
elif( self.color == 'cyan' ):
y1 += 3 * 64
elif( self.color == 'purple' ):
y1 += 4 * 64
elif( self.color == 'random' ):
y1 += randrange(5) * 64
if printinfo:
print '%d : character = "%s", %s, %d, (%d,%d)' % (idx, character, key, ord( character ), x1, y1)
if( character == ' ' ):
sizex = space_sizex # make spaces smaller
else:
sizex = 8
crop_box = (x1+4,y1,x1+4+sizex,y1+sizey)
letter_img = img1.crop( crop_box )
ni.paste( letter_img, (paste_loc_x, 0, paste_loc_x+sizex, sizey) )
paste_loc_x += sizex
return( ni )
def createFroggerStringImage( self, mystr, printinfo=True ):
sizex = 16
sizey = 16
space_sizex = 4 # make space characters smaller
img1 = Image.open( "./sprite_sheets/frogger.png" )
ni_size_x = (len(mystr)*sizex) - (mystr.count(' ') * (sizex-space_sizex)) # make new image size take into account the smaller size of spaces
ni = Image.new( 'RGB', (ni_size_x, sizey) )
paste_loc_x = 0
paste_loc_y = 0
space_letter_coords = self.letter_coords.get('frogger_ ')
for idx, character in enumerate( mystr ):
key = 'frogger_' + character
(x1, y1) = self.letter_coords.get( key, space_letter_coords ) # get the coords asked for if they exist, else return coords for a space
if printinfo:
print '%d : character = "%s", %s, %d, (%d,%d)' % (idx, character, key, ord( character ), x1, y1)
if( character == ' ' ):
sizex = space_sizex # make spaces smaller
else:
sizex = 16
crop_box = (x1,y1,x1+sizex,y1+sizey)
letter_img = img1.crop( crop_box )
#ni.paste( letter_img, (idx*sizex, 0, (idx+1)*sizex, sizey) )
ni.paste( letter_img, (paste_loc_x, 0, paste_loc_x+sizex, sizey) )
paste_loc_x += sizex
return( ni )
# ================================================================================
if __name__ == '__main__':
if False:
debug = True
tb = TextBuilder();
string_image = tb.createStringImage( "CORY STEWART", debug );
grid = Ledgrid()
grid.showWideImage( string_image, duration_ms=6, printinfo=debug )
grid.clear()
grid.close()
sys.exit(0)
if True:
debug = False
tb = TextBuilder();
string_image = tb.createFroggerStringImage( " F R O G G E R ", debug );
grid = Ledgrid()
grid.showWideImage( string_image, duration_ms=0, printinfo=debug )
grid.clear()
grid.close()
sys.exit(0)