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

font.getsize() was deprecated and is removed but the new documented use with font.getbbox() is not compatible #7865

Closed
SamuelWiet opened this issue Mar 8, 2024 · 5 comments

Comments

@SamuelWiet
Copy link

SamuelWiet commented Mar 8, 2024

What did you do?

I upgrade Pillow from 9.5.0 to 10.2.0 and adjust sources according to the documentation regarding migration from font.getsize() to font.getbbox().

What did you expect to happen?

I expect same results.

What actually happened?

At least for height I got ohter results as before.
Upgrading from 9.5.0 to 10.2.0 it appears that former deprecated function font.getsize() is not replacable with font.getbbox() like described in the documentation.

What are your OS, Python and Pillow versions?

  • OS: Windows
  • Python: 3.11
  • Pillow: 9.5,0 and 10.2.0
from PIL import ImageFont

PATH_TO_TTF = "FreeMono.ttf"


def get_size_from_deprecated_function(font, s):
    return font.getsize(s)


def get_size_from_new_documented_use(font, s):
    left, top, right, bottom = font.getbbox(s)
    width, height = right - left, bottom - top
    size = width, height
    return size


def compare_deprecated_with_new_documented_usage():
    font = ImageFont.truetype(PATH_TO_TTF, 15)
    string_to_test = 'a'

    size_old = get_size_from_deprecated_function(font, string_to_test)
    size_new = get_size_from_new_documented_use(font, string_to_test)

    print('old: ', size_old)
    print('new: ', size_new)

    if size_old == size_new:
        print('okay')
    else:
        print('not okay')


compare_deprecated_with_new_documented_usage()
@nulano
Copy link
Contributor

nulano commented Mar 8, 2024

This is a duplicate of #7802.

@nulano nulano closed this as not planned Won't fix, can't repro, duplicate, stale Mar 8, 2024
@nulano
Copy link
Contributor

nulano commented Mar 8, 2024

For a 1:1 replacement you can use width, height = right - left, bottom.

However, you might want to consider whether that is actually the value you are looking for, since this ignores the top value.

@SamuelWiet
Copy link
Author

For a 1:1 replacement you can use width, height = right - left, bottom.

However, you might want to consider whether that is actually the value you are looking for, since this ignores the top value.

It has just compatibility reasons. Documentation should be updated.....

@radarhere
Copy link
Member

That is what is currently proposed as the solution to #7802, in #7806.

@nulano
Copy link
Contributor

nulano commented Mar 11, 2024

#7806 has now been merged, you can see the updated documentation here: https://pillow.readthedocs.io/en/latest/deprecations.html#font-size-and-offset-methods

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

No branches or pull requests

3 participants