Skip to content

Instantly share code, notes, and snippets.

@Ze1598
Last active July 28, 2020 13:16
Show Gist options
  • Save Ze1598/420c7eb600899c86d1d65e83c3cc8b25 to your computer and use it in GitHub Desktop.
Save Ze1598/420c7eb600899c86d1d65e83c3cc8b25 to your computer and use it in GitHub Desktop.
Calculate text size (Python, PIL)
def get_text_dimensions(text_string, font):
# https://stackoverflow.com/a/46220683/9263761
ascent, descent = font.getmetrics()
text_width = font.getmask(text_string).getbbox()[2]
text_height = font.getmask(text_string).getbbox()[3] + descent
return (text_width, text_height)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment