Skip to content

How to adjust font.getbbox to use top to bottom as a text height #8473

Answered by radarhere
AlexanderPodorov asked this question in Q&A
Discussion options

You must be logged in to vote

Does this suit your needs?

from PIL import Image, ImageFont

font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", size=48)
left, top, right, bottom = font.getbbox("Quick")
width, height = right - left, bottom - top
print(width, height)

It prints a height of 35, which matches the height you expect from

from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", size=48)
im = Image.new("RGB", (150, 50))
draw = ImageDraw.Draw(im)
draw.text((0, 0), "Quick", font=font)
im.save("out.png")

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@AlexanderPodorov
Comment options

Answer selected by AlexanderPodorov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants