# Fix "AttributeError: 'ImageDraw' object has no attribute 'textsize'" in Pillow 10

Yes. An agent replaces every draw.textsize call with textbbox arithmetic that gives the same numbers, proves it on every string the script measures, and runs it.

[Run this on Rush](https://cloud.getrush.ai/run/fix-imagedraw-textsize-attribute-error/pillow/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

Pillow 10.0.0, released on 1 July 2023, removed `ImageDraw.textsize`. Pillow 9.5.0 still answered it, with `DeprecationWarning: textsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use textbbox or textlength instead.` On 13.0.0, the current release, the call raises `AttributeError: 'ImageDraw' object has no attribute 'textsize'`. The warning names two replacements that measure differently from the old call and from each other. For the letter `F` in the Indie Flower font at 90 pixels, `textsize` returned `(60, 88)`, `textbbox((0, 0), ...)` returns `(-4, 34, 56, 88)`, and `textlength` returns `46.3125`. The old width is the box's right edge minus its left edge, 56 minus minus 4; the old height is the box's bottom edge, not bottom minus top; and `textlength` is the advance width, a float that rounds to the old width only sometimes. A replacement that takes the right edge alone, or rounds `textlength`, moves text by a few pixels on glyphs with a left bearing.

VighneshPath/Hand-Written-Notes, a script that writes a text file onto a scanned page in a handwriting font, letter by letter, is a clear case. Its `requirements.txt` is one line, `pillow`, unpinned. The repository ships everything the script reads: `page.jpeg`, `IndieFlower-Regular.ttf`, a `writing_text.txt` of Lorem Ipsum, and the `PageDim` shelf that holds the page measurements, so `python pp.py` runs with no input. Two lines measure. Line 102, `temp_width = draw.textsize(text[index:i], myfont)[0]`, checks whether the next word fits on the line; line 142, `lwidth += draw.textsize(letter, myfont)[0]`, advances the cursor after each `draw.text`. Both read index 0, the width. On a fresh install, Pillow 13.0.0, the script fails at line 142 on the first letter. On Pillow 9.5.0 it writes three pages, `text1.png` to `text3.png`, and prints `DONE!`.

The agent reproduces both runs, then establishes the replacement on the version where both calls exist rather than from the documentation. On 9.5.0 it measures every distinct letter, word and fragment in `writing_text.txt` with the shipped font, 1,119 strings, plus the 501 space-to-space fragments line 102 measures, and compares: `textsize` width equals `textbbox` right minus left for every one of them, and `textsize` height equals the box's bottom for every one; the right edge alone matches 1,032 of the 1,119, and `round(textlength)` matches 971. So lines 102 and 142 become `b = draw.textbbox((0, 0), s, font=myfont)` followed by `b[2] - b[0]`, with a comment that says why not `textlength`. A script that used the height would take `b[3]`.

Then it runs the changed script on 13.0.0 and sees the three pages written, and runs it on 9.5.0 to compare with the original. The output files are not byte-identical between any two runs, because lines 136 and 137 of `pp.py` jitter each letter by `random.randint(0, 5)` pixels to look handwritten, so the comparison that holds is the one above, measurement by measurement, plus a run with `random.seed` fixed in scratch copies of the old and new code, which on 9.5.0 produce three byte-identical pages. The agent also notes, as findings and not fixes, that the README names `icomoon.ttf` and `page.jpg` while the code opens `IndieFlower-Regular.ttf` and `page.jpeg`, and that the PDF is saved to the Windows path `MyPdf\assignment.pdf`, which on Linux is a file with a backslash in its name.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "This script fails with \"AttributeError: 'ImageDraw' object has no attribute 'textsize'\" after Pillow was installed fresh. Read the dependency files and report how Pillow is pinned, then create a clean virtual environment on a current Python, install the dependencies as a new user would, and report the Pillow version that was resolved. Run the script on the inputs the repository ships, or on stand-ins you describe if it ships none, and save the traceback. Search every .py file for textsize( and getsize( and list each call with its file and line and whether the code uses the width, the height, or both. Install Pillow 9.5.0 in a second environment, where textsize still exists, and measure every string the script measures with the same font to establish which textbbox or textlength expression reproduces the old width and height exactly; report the counts. Replace each call with that expression and a short comment. Do not pin Pillow below 10 and do not add hasattr branches. Run the changed script on the current Pillow and confirm it writes its output; if the script uses randomness, fix the seed in a scratch copy to compare the old and new output images on 9.5.0 and report whether they are identical. Open a pull request with the resolved versions, the list of changed calls, the measurement counts, and the comparison.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request that changes each measuring call and nothing else, two lines in `pp.py` in the fixture above. The description lists the Pillow version a clean install resolved, the traceback before, every `textsize(` and `getsize(` found with its file, line and which dimension it read, the counts from the 9.5.0 measurement of every string the script measures, and the run on 13.0.0 that writes the three pages.

What a reviewer looks at is the counts. A replacement that measures differently moves letters, so the description states the expression chosen, right minus left for the width, and that it matched the old value on every one of the strings measured, against the lower matches for the right edge alone and for `textlength`. For a script with randomness the description also shows the seeded comparison on 9.5.0, where old and new code produce the same files.

Findings the agent did not act on are listed separately: README file names that differ from the code, a Windows-only output path, and any `getsize` on a font object, which Pillow 10 also removed and which has its own replacement in `font.getbbox`.

## Run it on every Monday morning

An unpinned `pillow` line breaks on the day of an install, not on a commit. Give the Project an automation with a `schedule` trigger, for example the cron expression `0 7 * * 1` in your timezone, and a prompt that tells the agent to install the dependencies fresh, run each image-producing script on the inputs it ships or a small example, and open a pull request only when one fails on an API a new release removed. On a week when nothing broke, the run ends with a one-line answer and no pull request.

For a set of image tools, bots and generators that share this Pillow shape, start the request above once per repository from a script with an API key, and read each session's `pr_url` when its `run.completed` webhook arrives. Each run resolves its own Pillow in its own sandbox, and runs past the key's `max_concurrent` limit are accepted and wait as `queued`.

## Why run it on Rush?

The rename is not the hard part; choosing the expression that does not move the text is, and the documentation does not settle it, because the two suggested replacements disagree with the old call on glyphs with a left bearing. The honest way to settle it is to measure every string the script measures on a Pillow where both calls exist. An agent in a sandbox installs both versions, runs the measurement, and keeps the counts and the output pages on the session, so a reviewer can look rather than trust.

It also runs where the error lives. A laptop that has had Pillow 9 since the script was written never raises it. A sandbox installs what `pip install pillow` gives today, which is the only environment where line 142 fails, and the one the next user of the script will have.

## Frequently asked questions

### Which replacement should I use, textbbox or textlength?

For code that read the width as an integer, `textbbox((0, 0), text, font=font)` and then right minus left: on the fixture's font that equals the old width for every string measured, while the right edge alone and `round(textlength)` each miss some. For a height, use the box's bottom edge, which is what `textsize` returned, not bottom minus top.

### Can I pin Pillow to 9.5 instead?

It works until something else needs Pillow 10 or a Python that 9.5 publishes no wheels for, and 9.5.0 already warns on every call that the method will go. The request above tells the agent not to pin; the change is two lines.

### Why does the agent compare measurements instead of output images?

Because this script jitters every letter by a random pixel or two, so no two runs produce the same file even on the same Pillow. The measurements are what the layout depends on, and they can be compared exactly; a seeded scratch copy then shows identical pages as a second check.

### I also get 'FreeTypeFont' object has no attribute 'getsize'. Same fix?

Same release, different method. Pillow 10 removed `font.getsize` too; its replacement is `font.getbbox`, with the same right minus left arithmetic for the width, and `font.getlength` for the float advance. The agent lists those calls in the same search and changes them the same way.

### Does the README mismatch get fixed?

It is reported, not changed. The README asks for `icomoon.ttf` and `page.jpg` while `pp.py` opens `IndieFlower-Regular.ttf` and `page.jpeg`, both of which the repository ships. Which name to keep is the author's call, so the pull request lists it as a finding.

## More Pillow jobs

- [Fix "module 'PIL.Image' has no attribute 'ANTIALIAS'" After a Pillow Upgrade](https://getrush.ai/run/fix-pil-image-antialias-error/pillow/)
- [Fix "AttributeError: 'FreeTypeFont' object has no attribute 'getsize'" in Pillow 10](https://getrush.ai/run/fix-freetypefont-getsize-attribute-error/pillow/)

## More jobs like this: Fix a bug from an alert

- [Fix imageio GIFs That Play With No Frame Delay After Upgrading (mimsave duration)](https://getrush.ai/run/fix-imageio-mimsave-gif-duration-ignored/imageio/)
- [Fix "SyntaxError: Unexpected identifier 'assert'" in rollup.config.js on Node.js 22](https://getrush.ai/run/fix-import-assertions-syntax-error/rollup/)
- [Fix "error TS5102: Option 'importsNotUsedAsValues' has been removed" in tsconfig.json](https://getrush.ai/run/fix-importsnotusedasvalues-removed/typescript/)
- [Fix "ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape"](https://getrush.ai/run/fix-inhomogeneous-shape-value-error/numpy/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
