-->
Fixing attributeerror: 'freetypefont' object has no attribute 'getsize' and AttributeError: 'FreeTypeFont object has no attribute 'read Python

Fixing attributeerror: 'freetypefont' object has no attribute 'getsize' and AttributeError: 'FreeTypeFont object has no attribute 'read Python

Back to top

Updated by Ashirafu Kibalama on September 17, 2024

Why are you seeing "attributeerror: 'freetypefont' object has no attribute 'getsize' and AttributeError: 'FreeTypeFont object has no attribute 'read" Python and How To fix?





The AttributeError: 'FreeTypeFont' object has no attribute 'getsize', and the AttributeError: 'FreeTypeFont' object has no attribute 'read' occurs when using older or incorrect methods on a FreeTypeFont object from the Pillow library in Python.


Let's break down each error and how to fix it:


1) AttributeError: 'FreeTypeFont' object has no attribute 'getsize' Python




Cause:

The getsize() method was removed in Pillow 6.0.0 and replaced with the getbbox() method. The error occurs when using the deprecated method on a FreeTypeFont object.


Solution:

Use the getbbox() method instead. The getbbox() method returns a bounding box for the given text, which you can then use to calculate the text size.


Here's an example of how to fix the code that might cause this error:


# Codes Before Fixing the AttributeError: 'FreeTypeFont' object has no attribute 'getsize' Python



# Other codes aboves

# Get text size using the deprecated getsize() method
text = "Hello, World!"
text_width, text_height = font.getsize(text)

# Other codes below



# Codes After Fixing the AttributeError: 'FreeTypeFont' object has no attribute 'getsize' Python



# Other codes aboves

# Get text size using the deprecated getsize() method
text = "Hello, World!"
# text_width, text_height = font.getsize(text)


bbox = draw.textbbox((0, 0), text, font=font)
text_width, text_height = bbox[2] - bbox[0], bbox[3] - bbox[1]

# Other codes below

# Draw text on image


So, in this code, you are changing this code line:



text_width, text_height = font.getsize(text)


With these two lines of code:



bbox = draw.textbbox((0, 0), text, font=font)
text_width, text_height = bbox[2] - bbox[0], bbox[3] - bbox[1]


This should resolve the AttributeError: 'FreeTypeFont' object has no attribute 'getsize' Python and allows your code to run correctly.


2) AttributeError: 'FreeTypeFont' object has no attribute 'read' Python




Cause:

The AttributeError: 'FreeTypeFont' object has no attribute 'read' error, which typically occurs when using a FreeTypeFont object as if it were a file-like object.


This error often arises if you mistakenly try to pass a FreeTypeFont object to a function that expects a file-like object, such as when using the ImageFont object with functions like ImageDraw.text() in the Pillow library.



Solution:

To fix the AttributeError: 'FreeTypeFont' object has no attribute 'read', ensure you pass the font object directly to the draw.text() method, without trying to use the .read() method on it.


Here's an example of how to fix the code that might cause this error:


# Codes Before Fixing the AttributeError: 'FreeTypeFont' object has no attribute 'read' Python


# Other codes above

# Create an image
image = Image.new("RGB", (200, 100), (255, 255, 255))
draw = ImageDraw.Draw(image)


draw.text((10, 10), "Hello, World!", font.read(), fill="black")

# Other codes below

# Save the image


# Codes After Fixing the AttributeError: 'FreeTypeFont' object has no attribute 'read' Python



# Other codes above

# Create an image
image = Image.new("RGB", (200, 100), (255, 255, 255))
draw = ImageDraw.Draw(image)


draw.text((10, 10), "Hello, World!", font=font, fill="black")

# Other codes below

# Save the image


So, in this code, you are changing this code line:



draw.text((10, 10), "Hello, World!", font.read(), fill="black")


With this line of code:



draw.text((10, 10), "Hello, World!", font=font, fill="black")



This should resolve the AttributeError: 'FreeTypeFont' object has no attribute 'read' Python and allows your code to run correctly.


Conclusion

In conclusion, addressing the AttributeError: 'FreeTypeFont' object has no attribute 'getsize' and AttributeError: 'FreeTypeFont' object has no attribute 'read' requires the use of correct methods on a FreeTypeFont object from the Pillow library in Python.


Happy coding!!!


Other Posts:

1 How to Fix AttributeError: 'dict_values' object has no attribute 'update_relative' in Python With Examples.


2 How do you Fix or Solve an Attribute Error if the 'str' Object has no Attribute in Python?


3 With an Example Fix: Os error Cannot Open Resource Python / OSError: Cannot Open Resource imagefont


4 With an Example Fix: AttributeError: 'ImageDraw' object has no attribute 'textsize' Python


5 How do we Compare Two Lists Using List Comprehension Python?


6 With an Example Fix: attributeerror: 'list' object has no attribute 'update_relative' Python