@@ -205,18 +205,19 @@ def DisplayText(
205205 # The text bitmap is created from provided background image : text with transparent background
206206 text_image = Image .open (background_image )
207207
208- # Draw text with specified color & font
208+ # Get text bounding box
209209 font = ImageFont .truetype ("./res/fonts/" + font , font_size )
210210 d = ImageDraw .Draw (text_image )
211+ left , top , text_width , text_height = d .textbbox ((0 , 0 ), text , font = font )
212+
213+ # Draw text with specified color & font, remove left/top margins
214+ d .text ((x - left , y - top ), text , font = font , fill = font_color )
211215
212216 # Crop text bitmap to keep only the text (also crop if text overflows display)
213- left , top , text_width , text_height = d .textbbox ((0 , 0 ), text , font = font )
214- d .text ((x - left , y - top ), text , font = font , fill = font_color )
215-
216217 text_image = text_image .crop (box = (
217218 x , y ,
218- min (x + text_width , self .get_width ()),
219- min (y + text_height , self .get_height ())
219+ min (x + text_width - left , self .get_width ()),
220+ min (y + text_height - top , self .get_height ())
220221 ))
221222
222223 self .DisplayPILImage (text_image , x , y )
0 commit comments