-
Notifications
You must be signed in to change notification settings - Fork 1
/
bingocard.py
346 lines (251 loc) · 10.4 KB
/
bingocard.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from PIL import Image, ImageDraw, ImageFont, _imaging
import random, sys, os
class BingoCard( object ):
@classmethod
def draw_several_cards( self, card, how_many=6, save_to=None, cols_per_image=3, background='white' ):
if card.image is None:
card.initialize_image()
image = card.image
rows = ( ( how_many - 1 ) // cols_per_image ) + 1
if how_many >= cols_per_image:
cols = cols_per_image
else:
cols = how_many
iw, ih, w, h = card.width, card.height, card.width * cols, card.height * rows
nimage = Image.new( 'RGB', ( w, h ), background )
for row in range( rows ):
for col in range( cols ):
embed_image = card.print_card( save_to=None )
nimage.paste( embed_image, ( col * iw, row * ih ) )
if save_to is None:
return nimage
else:
return nimage.save( save_to )
def _initialize_args( self ):
return [
'width',
'height',
'header',
'margin_top',
'margin_left',
'margin_right',
'margin_bottom',
'margin',
'with_ref_image',
'background',
'draw_lines',
'set_gap',
'set_length'
]
def _prepare_args_from_dictionary( self, _kwargs ):
new_kwargs = { }
for i in self._initialize_args():
if _kwargs.has_key( i ):
new_kwargs[ i ] = _kwargs[ i ]
return new_kwargs
def __init__( self, width=800, height=600, header=10, margin_top=10, margin_left=10, margin_right=10, margin_bottom=10, margin=None, with_ref_image=None, background='white', draw_lines=True, set_gap=0, set_length=1):
self.width = width
self.height = height
self.header = header
self.with_ref_image = with_ref_image
self.background = background
self.font = 'Overlock-Black.ttf'
self.draw_lines = draw_lines
self.line_size = 2
self.line_color = 0
self.text_size = None
self.text_color = 0
self.image = None
self.set_length = set_length
self.set_gap = set_gap
if margin is None and ( margin_left > 0 and margin_right > 0 and margin_bottom > 0 and margin_top > 0 ):
self.margin_left = margin_left
self.margin_right = margin_right
self.margin_bottom = margin_bottom
self.margin_top = margin_top
else:
margin = margin or 10
self.margin_top = self.margin_left = self.margin_right = self.margin_bottom = margin
def card_layout( self ):
return [ ]
def get_text_size( self ):
if self.text_size is None:
text_size = int( self.width / 38.4 )
else:
text_size = self.text_size
return text_size
def get_font( self ):
return ImageFont.truetype( self.font, self.get_text_size() )
def get_top( self ):
if self.header:
top = ( self.header ) + self.margin_top
else:
top = self.margin_top
return top
# FIXME: Doesn't deal with sets yet
def draw_lines_on_image( self, draw, layout, col_height, col_width ):
if self.draw_lines:
# Rows
for i in range( len( layout ) + 1 ):
y = ( self.get_top() + ( i * col_height ) )
draw.line( ( self.margin_left, y, self.width - self.margin_right, y ), fill=self.line_color, width=self.line_size )
# Cols
for j in range( len( layout[ 0 ] ) + 1 ):
x = self.margin_left + ( j * col_width )
draw.line( ( x, self.get_top(), x, self.height - self.margin_bottom ), fill=self.line_color, width=self.line_color )
def draw( self, layout, save_to ):
print "About to draw"
image, width, height = self.image, self.width, self.height
font, text_size, top = self.get_font(), self.get_text_size(), self.get_top()
ml, mr, mt, mb = self.margin_left, self.margin_right, self.margin_top, self.margin_bottom
sl, sg = self.set_length, self.set_gap
draw = ImageDraw.Draw( image )
col_height = ( height - top - mb - ( ( ( len( layout ) // sl ) - 1 ) * sg ) ) / len( layout )
col_width = ( width - ( ml + mr ) ) / len( layout[ 0 ] )
self.draw_lines_on_image( draw, layout, col_height, col_width )
# Start drawing numbers
for i in range( len( layout ) ):
for j in range( len( layout[0] ) ):
number = layout[ i ][ j ]
x = ml + ( j * col_width ) + ( col_width / 2 ) - ( text_size / 2 )
y = top + ( ( i // sl ) * sg ) + ( i * col_height ) + ( col_height / 2 ) - ( text_size / 2 )
if number is not None:
draw.text( ( x, y ), str( number ), fill=0, font=font )
print "OK"
# Save stuff
if save_to is None:
return image
else:
image.save( save_to )
def bingo_card( self ):
""" Needs to be implemented by the subclass """
def initialize_image( self ):
if( self.with_ref_image ):
self.image = im = Image.open( self.with_ref_image )
self.width, self.height = im.size
else:
self.image = Image.new( 'RGB', ( self.width, self.height ), self.background )
def print_card( self, save_to='test.png' ):
self.initialize_image()
return self.draw( self.bingo_card(), save_to )
class USBingoCard( BingoCard ):
def __init__( self, **kwargs ):
BingoCard.__init__( self, **self._prepare_args_from_dictionary( kwargs ) )
def card_layout( self ):
return [ range( i, i + 15 ) for i in range( 1, 75, 15 ) ]
def bingo_card( self ):
layout = self.card_layout()
num_of_rows = 5
cols = 5
blanks = [ ]
possible_numbers = range( 1, 76 )
for i in range( num_of_rows ):
row = [ ]
for j in range( cols ):
flag = True
while flag:
number = layout[ j ][ random.randint( 0, len( layout[ j ] ) - 1 ) ]
if number in possible_numbers:
possible_numbers.remove( number )
row.append( number )
flag = False
blanks.append( row )
blanks[ 2 ][ 2 ] = None
return blanks
class UKBingoCard( BingoCard ):
def __init__( self, **kwargs ):
BingoCard.__init__( self, **self._prepare_args_from_dictionary( kwargs ) )
if kwargs.has_key( 'set_size' ):
self.set_size = kwargs[ 'set_size' ]
else:
self.set_size = 1
def card_layout( self ):
return [
range( 1, 9 + 1 ),
range( 10, 19 + 1 ),
range( 20, 29 + 1),
range( 30, 39 + 1 ),
range( 40, 49 + 1),
range( 50, 59 + 1),
range( 60, 69 + 1),
range( 70, 79 + 1),
range( 80, 90 + 1)
]
def bingo_card( self ):
layout = self.card_layout()
num_of_rows = 3
cols = len( self.card_layout() )
blanks = [ ]
stack = range( 1, 91 )
seen = { }
for s in range( self.set_size ):
for i in range( num_of_rows ):
row = [ None ] * 9
published_cols = {}
for j in range( 5 ):
val = None
tries = 0
while val is None:
if tries > 20:
break
tries += 1
val = random.choice( stack )
for k in range( len( layout ) ):
if val in layout[ k ]:
if (published_cols.has_key( k ) and published_cols[ k ] < 4):
val = None
if not seen.has_key( val ):
for k in range( len( layout ) ):
if val in layout[ k ]:
if not published_cols.has_key( k ) or (published_cols.has_key( k ) and published_cols[ k ] < 4):
row[ k ] = val
if not published_cols.has_key( k ):
published_cols[ k ] = 1
else:
published_cols[ k ] += 1
seen[ val ] = True
stack.remove( val )
break
else:
pass
else:
val = None
blanks.append( row )
if len(stack) > 0 and self.set_size == 6:
return self.bingo_card()
else:
return blanks
if __name__ == '__main__':
# A single US Card - Indigo
o = USBingoCard( header=90, margin_top=35, margin_left=60, margin_right=65, margin_bottom=100, with_ref_image='templates/75/indigo.jpg', draw_lines=False )
o.print_card( 'test.png' )
# A single US Card - Blue
o = USBingoCard( header=90, margin_top=35, margin_left=60, margin_right=65, margin_bottom=100, with_ref_image='templates/75/blue.jpg', draw_lines=False )
o.print_card( 'test1.png' )
# A single US Card - Without template
o = USBingoCard( )
o.print_card( 'test2.png' )
# 4 US Cards - Red
o = USBingoCard( header=90, margin_top=35, margin_left=60, margin_right=65, margin_bottom=100, with_ref_image='templates/75/red.jpg', draw_lines=False )
BingoCard.draw_several_cards( o, how_many=4, save_to='test3.png', cols_per_image=2 )
# 6 US Cards - Yellow
o = USBingoCard( header=90, margin_top=35, margin_left=60, margin_right=65, margin_bottom=100, with_ref_image='templates/75/yellow.jpg', draw_lines=False )
BingoCard.draw_several_cards( o, how_many=6, save_to='test4.png', cols_per_image=3 )
# A Single UK Card - Indigo
o = UKBingoCard( header=50, margin_top=40, margin_left=35, margin_right=38, margin_bottom=42, with_ref_image='templates/90/1/indigo.jpg', draw_lines=False)
o.print_card( 'test5.png' )
# A Single UK Card - Blue
o = UKBingoCard( header=50, margin_top=40, margin_left=35, margin_right=38, margin_bottom=42, with_ref_image='templates/90/1/blue.jpg', draw_lines=False)
o.print_card( 'test5.png' )
# A Single UK Card - Without template
o = UKBingoCard()
o.print_card( 'test6.png' )
# A Six Set of individual cards, no unique numbers in a column - Yellow
o = UKBingoCard( header=50, margin_top=40, margin_left=35, margin_right=38, margin_bottom=42, with_ref_image='templates/90/1/yellow.jpg', draw_lines=False)
BingoCard.draw_several_cards( o, how_many=6, cols_per_image=1, save_to='test7.png' )
# A Six-set UK Bingo card with unique numbers in columns - Red
o = UKBingoCard( set_size=6, set_gap=62.5, set_length=3, header=62.5, margin_top=1, margin_left=100, margin_right=105, margin_bottom=75, with_ref_image='templates/90/6/red.jpg', draw_lines=False)
o.print_card( 'test8.png' )
# A Six-set UK Bingo card with unique numbers in columns - Violet - Printed Several times
o = UKBingoCard( set_size=6, set_gap=62.5, set_length=3, header=62.5, margin_top=1, margin_left=100, margin_right=105, margin_bottom=75, with_ref_image='templates/90/6/violet.jpg', draw_lines=False)
BingoCard.draw_several_cards( o, how_many=6, cols_per_image=1, save_to='test9.png' )