@@ -130,14 +130,23 @@ def _init_image(self):
130
130
self .image = Image .new (mode = "RGB" , size = (size , size ))
131
131
self .draw = ImageDraw .Draw (self .image )
132
132
133
- def _render_square_background (self , x , y ):
133
+ def _get_square_at (self , x , y ):
134
+ # since chess.SQUARES starts at a1
135
+ return chess .SQUARES [x + (7 - y ) * 8 ]
136
+
137
+ def _render_square_background (self , x , y , highlighted_squares = ()):
134
138
rectx = x * self .square_size
135
139
recty = y * self .square_size
136
140
colors = self .square_config .color
141
+ highlight_colors = self .square_config .highlight_color
142
+
143
+ fill_color = colors [(x + y ) % 2 ]
144
+ if self ._get_square_at (x , y ) in highlighted_squares :
145
+ fill_color = highlight_colors [(x + y ) % 2 ]
137
146
138
147
self .draw .rectangle (
139
148
(rectx , recty , rectx + self .square_size , recty + self .square_size ),
140
- fill = colors [( x + y ) % 2 ] ,
149
+ fill = fill_color ,
141
150
)
142
151
143
152
def _render_square_location (self , x , y ):
@@ -171,19 +180,18 @@ def _render_piece(self, x, y):
171
180
rectx = x * self .square_size
172
181
recty = y * self .square_size
173
182
174
- # since chess.SQUARES starts at a1
175
- square = chess .SQUARES [x + (7 - y ) * 8 ]
183
+ square = self ._get_square_at (x , y )
176
184
piece = self .board .piece_at (square )
177
185
if piece is not None :
178
186
piece_image = self .piece_drawer .render (piece )
179
187
self .image .paste (piece_image , (rectx , recty ), piece_image )
180
188
181
- def render (self ):
189
+ def render (self , * highlighted_squares : chess . Square ):
182
190
self ._init_image ()
183
191
184
192
for x in range (8 ):
185
193
for y in range (8 ):
186
- self ._render_square_background (x , y )
194
+ self ._render_square_background (x , y , highlighted_squares )
187
195
if self .text_config .enabled :
188
196
self ._render_square_location (x , y )
189
197
self ._render_piece (x , y )
@@ -192,6 +200,13 @@ def render(self):
192
200
193
201
194
202
if __name__ == "__main__" :
203
+ from timeit import default_timer as timer
204
+
195
205
fen = "rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2"
196
- image = FenToImage (fen , Config ()).render ()
206
+
207
+ start = timer ()
208
+ image = FenToImage (fen , Config ()).render (chess .C7 , chess .C5 )
209
+ end = timer ()
210
+
211
+ print ("rendered in" , end - start , "seconds" )
197
212
image .show ()
0 commit comments