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

Fix issue 175 by replacing deprecated getsize function #180

Merged
merged 1 commit into from
Nov 20, 2023

Conversation

trepidacious
Copy link
Contributor

Fix #175

font.getsize can be replaced with a simple function calling through to font.getbbox. This seems to give very similar results, testing with a few quotes the size was almost always identical, sometimes different by 1 pixel.

This allows the updated examples to work with Pillow 10.x

@neozenith
Copy link

neozenith commented Nov 15, 2023

Can confirm the getbbox works with Inky pHAT Red and pillow==10.1.0.

Since:

  • getsize was deprecated in 9.2.0 and end-of-life <10
  • getbbox was only made available from 9.2.0

https://pillow.readthedocs.io/en/stable/releasenotes/9.2.0.html#font-size-and-offset-methods

Would the following help?

def getsize(font, text):
    try:
        # pillow>=9.2.0
        # https://pillow.readthedocs.io/en/stable/releasenotes/9.2.0.html#font-size-and-offset-methods
        _, _, right, bottom = font.getbbox(text)
        return (right, bottom)
    except AttributeError:
        # Legacy fallback method
        # pillow<10
        return font.getsize(text)

@Gadgetoid Gadgetoid merged commit 6162b5f into pimoroni:main Nov 20, 2023
2 checks passed
@Gadgetoid
Copy link
Member

Thank you!

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

Successfully merging this pull request may close these issues.

AttributeError: 'FreeTypeFont' object has no attribute 'getsize'
3 participants