tensorflow/models

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

PawZawDev opened this issue ยท 8 comments

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
Sid220 commented

Same issue

Same issue

pip install Pillow==9.5.0

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.

Hi @PawZawDev,

A pull request that we submitted has been successfully merged.

Closing this as completed.

Thanks.

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

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)

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)

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