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

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

Back to top

Updated by Ashirafu Kibalama on September 17, 2024

Fixing: AttributeError: 'ImageDraw' object has no attribute 'textsize' in Python




The AttributeError: 'ImageDraw' object has no attribute 'textsize' occurs because the textsize method was deprecated in Pillow (a popular Python Imaging Library fork) and replaced with textbbox or textlength methods.


Here's how you can fix AttributeError: 'ImageDraw' object has no attribute 'textsize' Python with an example:



#Code before fixing the error:

# Calculate the position to center the text

text_width, text_height = d.textsize(text, font=font)
position = ((img.width - text_width) // 2, (img.height - text_height) // 2)

# Add text to image
d.text(position, text, fill=color, font=font)

# Save the logo


#Code After fixing the error:


# Calculate the position to center the text

bbox = d.textbbox((0, 0), text, font=font)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
# text_width, text_height = d.textsize(text, font=font)
position = ((img.width - text_width) // 2, (img.height - text_height) // 2)

# Add text to image
d.text(position, text, fill=color, font=font)

# Save the logo


We have replaced this code line:


text_width, text_height = d.textsize(text, font=font)


with this code:


# Calculate the position to center the text

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


The ImageDraw class in Pillow no longer has the textsize method. Instead, you should use the textbbox method, which returns a bounding box for the text. You can calculate the width and height from this bounding box.


Calculating Text Width and Height

text_width = bbox[2] - bbox[0]text_width = bbox[2] - bbox[0]

    • bbox[2] is the right coordinate, and bbox[0] is the left coordinate.
    • Subtracting these two gives the width of the bounding box (i.e., how wide the text is).

    text_height = bbox[3] - bbox[1]text_height = bbox[3] - bbox[1]

      • bbox[3] is the bottom coordinate, and bbox[1] is the top coordinate.
      • Subtracting these two gives the height of the bounding box (i.e., how tall the text is).


      Conclusion:

      By understanding and implementing the fix using textbbox method, you not only resolve the error but also gain access to more precise text dimension measurements.

      This will fix the AttributeError and allow you to correctly calculate the text size using the newer method in Pillow.


      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 Fixing attributeerror: 'freetypefont' object has no attribute 'getsize' and AttributeError: 'FreeTypeFont object has no attribute 'read Python


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


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