Skip to content

Commit 6e18ed1

Browse files
authored
Merge pull request #20 from mathoudebine/fix/18-textsize
Fix deprecated PIL function ImageDraw.textsize
2 parents 4278f69 + 82f571a commit 6e18ed1

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ def DisplayText(ser: serial.Serial, text: str, x=0, y=0,
117117
# The text bitmap is created from provided background image : text with transparent background
118118
text_image = Image.open(background_image)
119119

120-
# Draw text with specified color & font
120+
# Draw text with specified color & font (also crop if text overflows display)
121121
font = ImageFont.truetype("./res/fonts/" + font, font_size)
122122
d = ImageDraw.Draw(text_image)
123123
d.text((x, y), text, font=font, fill=font_color)
124124

125125
# Crop text bitmap to keep only the text
126-
text_width, text_height = d.textsize(text, font=font)
126+
left, top, text_width, text_height = d.textbbox((0,0), text, font=font)
127127
text_image = text_image.crop(box=(x, y, min(x + text_width, DISPLAY_WIDTH), min(y + text_height, DISPLAY_HEIGHT)))
128128

129129
DisplayPILImage(ser, text_image, x, y)

0 commit comments

Comments
 (0)