Home / Errors / textsize-removed

ImageDraw object has no attribute textsize

Pillow 10 removed draw.textsize(). Use draw.textbbox() or draw.textlength(), which return different things.

AttributeError: 'ImageDraw' object has no attribute 'textsize'

The fix

w, h = draw.textsize("hello", font=font)

left, top, right, bottom = draw.textbbox((0, 0), "hello", font=font)
w = right - left
h = bottom - top

Why it is not a straight rename

textsize returned a width and a height. textbbox returns a four value box relative to the anchor point you pass in. The extra information is useful, because text has parts that hang below the baseline and the old call could not tell you about them, but it does mean the surrounding code has to change.

If you only need the width

w = draw.textlength("hello", font=font)

This returns a float and is the right call for laying out a single line.

Centring without the arithmetic

Most code that measured text was doing it to centre the text. The anchor argument removes the need:

draw.text((200, 150), "hello", font=font, fill="black", anchor="mm")

mm means middle horizontally and middle vertically, so the coordinate you give becomes the centre of the text rather than its top left corner.

The font method too

ImageFont.getsize() went in the same release. Use font.getbbox(text) or font.getlength(text).

See the ImageDraw module for the rest.

Still stuck

The PIL to Pillow converter finds most of these automatically, and the other error pages cover the rest.