Last active
July 28, 2020 13:16
-
-
Save Ze1598/420c7eb600899c86d1d65e83c3cc8b25 to your computer and use it in GitHub Desktop.
Calculate text size (Python, PIL)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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