Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getbbox incorrect when string contains new line character #7013

Closed
vanviethieuanh opened this issue Mar 14, 2023 · 3 comments
Closed

getbbox incorrect when string contains new line character #7013

vanviethieuanh opened this issue Mar 14, 2023 · 3 comments

Comments

@vanviethieuanh
Copy link

What did you do?

So I use the getbbox to calculate the width and height of my string with a specific font. The string includes the \n character and it split the line into 2.

What did you expect to happen?

The string renders well and has a width of about 7xx pixels which is what I want. And I expect the getbbox function returns the same value.

What actually happened?

However, the result I got from getbbox is 1555 pixels which might be the width of no break line text.

What are your OS, Python, and Pillow versions?

  • OS: Windows 10 Enterprise
  • Python: Python 3.10.10
  • Pillow: 9.4.0
text = "Age is an issue of mind over matter.\nIf you don't mind, it doesn't matter."
_, _, text_width, text_height = FONT.getbbox(text)
# text_width = 1555

image
The size of the image is 1080*1080 so there is no way that the text width is 1555.

Thank you!

@nulano
Copy link
Contributor

nulano commented Mar 14, 2023

Line breaking is handled only in ImageDraw and not in ImageFont; you should use ImageDraw.multiline_textbbox(). The thread around #5816 (comment) might interest you.

@radarhere radarhere changed the title getbbox wrong calculate when string contains new line character (\n) getbbox incorrect when string contains new line character Mar 14, 2023
@radarhere
Copy link
Member

@vanviethieuanh did that answer your question?

@vanviethieuanh
Copy link
Author

@radarhere yes, it does! Thanks (。・ω・。)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants