pimoroni / inky

Combined library for V2/V3 Inky pHAT and Inky wHAT.
https://shop.pimoroni.com/?q=inky
MIT License
575 stars 121 forks source link

Fix issue 175 by replacing deprecated getsize function #180

Closed trepidacious closed 9 months ago

trepidacious commented 9 months ago

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 commented 9 months ago

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

Since:

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 commented 9 months ago

Thank you!