Skip to content

AttributeError: 'FreeTypeFont' object has no attribute 'getsize' (Pillow 10) #11040

Closed

Description

@PawZawDev

Prerequisites

  • [ X ] I am using the latest TensorFlow Model Garden release and TensorFlow 2.
  • [ X ] I am reporting the issue to the correct repository. (Model Garden official or research directory)
  • [ X ] I checked to make sure that this issue has not been filed already.

1. The entire URL of the file you are using

https://github.com/tensorflow/models/tree/master/official/vision/utils/object_detection/visualization_utils.py

2. Describe the bug

in line 208:
display_str_heights = [font.getsize(ds)[1] for ds in display_str_list]

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

This is caused by new version of Pillow (10) being installed by pip install tf-models-official which
removed the getsize function
Downgrading to Pillow 9.5 solved the problem

3. Steps to reproduce

follow tutorial up to https://www.tensorflow.org/tfmodels/vision/object_detection#visualization_of_train_data

4. Expected behavior

Images should be displayed

5. System information

  • Windows 10
  • TensorFlow installed from pip
  • TensorFlow version 2.10
  • Python version: 3.10

Activity

  1. added
    models:official
    type:bug
    on Jul 4, 2023
  2. Sid220 commented on Jul 10, 2023

    @Sid220

    Same issue

  3. Digital2Slave commented on Jul 11, 2023

    @Digital2Slave

    Same issue

    pip install Pillow==9.5.0

  4. laxmareddyp commented on Jul 11, 2023

    @laxmareddyp
    Collaborator

    Hi @PawZawDev,

    We have noticed that the most recent version of the Pillow library has removed the getsize method. As a result, we are currently working on adapting our codebase to accommodate this change. In the meantime, to avoid encountering this error in your environment, we recommend installing version 9.5.0 of Pillow by using the command pip install Pillow<=9.5.0. This will allow you to continue your work smoothly until the necessary adjustments are made.

    Hope you understand!

    Thanks.

  5. added a commit that references this issue on Jul 22, 2023
  6. laxmareddyp commented on Jul 24, 2023

    @laxmareddyp
    Collaborator

    Hi @PawZawDev,

    A pull request that we submitted has been successfully merged.

    Closing this as completed.

    Thanks.

  7. google-ml-butler commented on Jul 24, 2023

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

  8. tetrapod0 commented on Aug 21, 2023

    @tetrapod0

    tested in 9.5.0

    font = ImageFont.load_default()
    font.getsize('test')
    (24, 11)
    

    DeprecationWarning: getsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox or getlength instead.

    font = ImageFont.load_default()
    font.getbbox('test')
    (0, 0, 24, 11)
    
  9. IshuSinghSE commented on Sep 26, 2023

    @IshuSinghSE

    now in pillow 10 you can use

    Instead of ImageFont.FreeTypeFont(fontfile, size=55, encoding="unic") and getsize(string)

    use ImageFont.truetype(fontfile, size=55, encoding="unic") and getlength(string)

  10. BennisonDevadoss commented on Dec 11, 2023

    @BennisonDevadoss

    Same issue

    pip install Pillow==9.5.0

    Here I have a question, what is the way to resolve this, without downgrade the version of the Pillow to 9.5.0

  11. weixianwei0129 commented on Jul 30, 2024

    @weixianwei0129
            if pl_version < '9.5.0':  # 9.5.0 later
                left, top, right, bottom = font.getbbox(text)
                tw, th = right - left, bottom - top
            else:
                tw, th = font.getsize(text)
    
  12. bighammer-link commented on Nov 9, 2024

    @bighammer-link

    Same issue

    pip install Pillow==9.5.0

    Thanks a lot! I sloved my problems with this way!!

  13. commadelimited commented on Aug 9, 2026

    @commadelimited

    Someone posted a quick fix for this issue over on Stack Overflow.

    # old way using getsize()
    # text_width, text_height = font.getsize(text)
    # using pillow 10+
    left, top, right, bottom = font.getbbox(text)
    text_width = right - left
    text_height = bottom - top
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

models:officialtype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions