Skip to content

Commit

Permalink
Merge pull request #20 from mathoudebine/fix/18-textsize
Browse files Browse the repository at this point in the history
Fix deprecated PIL function ImageDraw.textsize
  • Loading branch information
mathoudebine authored Aug 22, 2022
2 parents 4278f69 + 82f571a commit 6e18ed1
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ def DisplayText(ser: serial.Serial, text: str, x=0, y=0,
# The text bitmap is created from provided background image : text with transparent background
text_image = Image.open(background_image)

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

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

DisplayPILImage(ser, text_image, x, y)
Expand Down

0 comments on commit 6e18ed1

Please sign in to comment.