Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

Pillow says ImageDraw has no textsize, and textbbox fails too

Your error

AttributeError: 'ImageDraw' object has no attribute 'textsize'2 more lines it can show

Fix it fast

Most common cause, 1 of 4

Your code still calls draw.textsize or draw.multiline_textsize

Measure with a bounding box instead. textbbox takes an anchor point first, then the string, and gives back four edges; width and height come from subtracting them.

Fix
from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("DejaVuSans.ttf", 32)
im = Image.new("RGB", (400, 100), "white")
draw = ImageDraw.Draw(im)

left, top, right, bottom = draw.textbbox((0, 0), "Hello world", font=font)
width, height = right - left, bottom - top
only_width = draw.textlength("Hello world", font=font)

# text with line breaks
l, t, r, b = draw.multiline_textbbox((0, 0), "Hello\nworld", font=font)
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

AttributeError: 'ImageDraw' object has no attribute 'textsize' appears on Pillow 10.0 and later, where textsize, multiline_textsize and font.getsize were removed. Call draw.textbbox((0, 0), text, font=font), unpack four numbers (left, top, right, bottom) and subtract to get width and height, or use draw.textlength for width alone. It holds through Pillow 13.

  • Applies to Pillow 13.0, Pillow 10.0 to 12.x, Python 3.11 and later
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the installed Pillow version and the packages that pull it in. Version 10 or higher means the old size methods are gone; a name under Required-by may be the code making the old call.

Run this first
python -m pip show pillow

Then find the line you see in this list and open its fix:

  1. 1Your code still calls draw.textsize or draw.multiline_textsizeThe last line of the traceback points to a file in your own project, not to site-packages
  2. 2textbbox was swapped in with the old textsize argumentsAfter the rename, Python complains that the 'text' argument is missing, or shows ValueError: too many values to unpack (expected 2)
  3. 3The font object's getsize or getoffset is calledThe error names FreeTypeFont, ImageFont or TransposedFont instead of ImageDraw
  4. 4A dependency was written for Pillow 9 and calls textsize itselfThe traceback ends inside site-packages, in a package such as a diagram or Stream Deck tool, and your own code has no textsize call

What does the error look like?

  • AttributeError: 'ImageDraw' object has no attribute 'textsize'
  • AttributeError: 'FreeTypeFont' object has no attribute 'getsize'
  • DeprecationWarning: textsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use textbbox or textlength instead.

Cause 1 of 4

Your code still calls draw.textsize or draw.multiline_textsize

You seeThe last line of the traceback points to a file in your own project, not to site-packages

Check

On Pillow 10 or newer, 'ImageDraw' object has no attribute 'textsize' means the method itself was deleted. Pillow deprecated it in 9.2.0 and removed it, along with multiline_textsize, in 10.0.0.

Check
grep -rnE "textsize|multiline_textsize" --include=*.py .

Fix

Measure with a bounding box instead. textbbox takes an anchor point first, then the string, and gives back four edges; width and height come from subtracting them. When only the width matters, textlength returns it directly.

Fix
from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("DejaVuSans.ttf", 32)
im = Image.new("RGB", (400, 100), "white")
draw = ImageDraw.Draw(im)

left, top, right, bottom = draw.textbbox((0, 0), "Hello world", font=font)
width, height = right - left, bottom - top
only_width = draw.textlength("Hello world", font=font)

# text with line breaks
l, t, r, b = draw.multiline_textbbox((0, 0), "Hello\nworld", font=font)

Verify

The script runs to the end and prints positive numbers for width and height.

Verify
python -c "from PIL import Image, ImageDraw; d = ImageDraw.Draw(Image.new('RGB', (10, 10))); print(d.textbbox((0, 0), 'Hi'))"

Confirmed by pillow.readthedocs.io

Did this fix it?

Cause 2 of 4

textbbox was swapped in with the old textsize arguments

You seeAfter the rename, Python complains that the 'text' argument is missing, or shows ValueError: too many values to unpack (expected 2)

Check

The new call is not a drop-in rename. ImageDraw.textbbox needs xy as its first parameter, so draw.textbbox(text, font) leaves the text slot empty. It also returns (left, top, right, bottom), so writing w, h = draw.textbbox(...) breaks on the unpack.

Check
grep -rn "textbbox(" --include=*.py .

Fix

Pass an anchor such as (0, 0) before the string and unpack all four values. To centre text, skip the arithmetic: give draw.text the middle point and anchor="mm", which also handles fonts whose glyphs start below the top edge.

Fix
# before (Pillow 9 and older)
# w, h = draw.textsize(msg, font=font)
# draw.text(((W - w) / 2, (H - h) / 2), msg, font=font)

W, H = im.size
draw.text((W / 2, H / 2), msg, font=font, anchor="mm")

Verify

The text lands in the middle of the image, and no TypeError or ValueError is raised.

Verify
python make_image.py && echo ok

Confirmed by pillow.readthedocs.io

Did this fix it?

Cause 3 of 4

The font object's getsize or getoffset is called

You seeThe error names FreeTypeFont, ImageFont or TransposedFont instead of ImageDraw

Check

Font-level helpers were removed in the same release. FreeTypeFont.getsize, getoffset and getsize_multiline no longer exist from Pillow 10.0.0, and a maintainer replied to the bug report that the removal was deliberate.

Check
grep -rnE "\.getsize\(|getsize_multiline|getoffset" --include=*.py .

Fix

Ask the font for its box with getbbox, or for the advance width with getlength. For text that spans several lines, draw.multiline_textbbox replaces getsize_multiline.

Fix
left, top, right, bottom = font.getbbox("Hello world")
width, height = right - left, bottom - top
advance = font.getlength("Hello world")

Verify

Both values print without an AttributeError.

Verify
python -c "from PIL import ImageFont; f = ImageFont.load_default(); print(f.getbbox('Hi'), f.getlength('Hi'))"

Confirmed by github.com

Did this fix it?

Cause 4 of 4

A dependency was written for Pillow 9 and calls textsize itself

You seeThe traceback ends inside site-packages, in a package such as a diagram or Stream Deck tool, and your own code has no textsize call

Check

Pillow's issue tracker has reports of documentation builds and desktop tools breaking the day 10.0.0 came out, because those packages, not the user, still used textsize. pip show pillow lists them under Required-by.

Check
python -m pip show pillow | grep Required-by

Fix

Upgrade that package first and see whether its newer release measures text with textbbox. If no fixed release exists, hold Pillow below 10 for that environment only. Pillow 9.5.0 has wheels for CPython 3.7 to 3.12, so newer interpreters would compile it from source.

Fix
python -m pip install --upgrade <the-package-in-the-traceback>
# stopgap only, in a separate virtual environment:
python -m pip install "pillow<10"

Verify

The command that failed now finishes, and pip reports the Pillow version you meant to run.

Verify
python -m pip show pillow | grep Version

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Read the DeprecationWarning lines in test logs: Pillow aims to warn for at least a year before a major release deletes a method.
  • Pin Pillow to a major version in requirements and read the deprecations page before bumping it.
  • Centre or align text through the anchor argument of draw.text rather than computing offsets from measured sizes.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.