Pillow: ImageDraw.textsize zero length error

Created on 3 Oct 2017  Â·  9Comments  Â·  Source: python-pillow/Pillow

What did you do?

Attempted to get the textsize of an empty string ("")

What did you expect to happen?

Returns a tuple which is the size of the text

What actually happened?

Produced this error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/PIL/ImageDraw.py", line 263, in textsize
    return font.getsize(text, direction, features)
  File "/usr/local/lib/python3.6/site-packages/PIL/ImageFont.py", line 111, in getsize
    return self.font.getsize(text)
SystemError: <built-in method getsize of ImagingFont object at 0x2c72760> returned NULL without setting an error

What versions of Pillow and Python are you using?

Pillow 4.30
Python 3.6

I believe this can be traced to the latest 4.3 release and this commit: https://github.com/python-pillow/Pillow/commit/fd0e7be55a77dd1d499c0b9df99d24e23e8c15e3

The following code is producing the error: https://github.com/python-pillow/Pillow/blob/46845bafc544bc117de2743c3d0a509b312f0442/_imaging.c#L2304-L2322

In previous versions, the function would not error. If it's not intended to get the size of an empty string, maybe changing the behavior of ImageDraw._multiline_split would fix the issue.

Code

import PIL.Image, PIL.ImageDraw
i = PIL.Image.new('RGB', (2000, 1000))
d = PIL.ImageDraw.Draw(i)
d.textsize("")
Bug Regression

All 9 comments

Just noticed https://github.com/python-pillow/Pillow/issues/2614
sorry, should have checked the issues better.

2614 is for ImageFont.getsize, this is for ImageDraw.Draw.textsize, so re-opening.

I can reproduce this with Pillow 4.3.0 in Python 3.6.0 (but not Python 2.7.13) on macOS Sierra:

from PIL import Image, ImageDraw
im = Image.new('RGB', (200, 100))
draw = ImageDraw.Draw(im)
draw.textsize("")
print("done")
[hugo:/tmp] 1 % python 2783.py
done



md5-ef21c2e4bb498804cc73dcd262eba499



⌂68% [hugo:/tmp] % python3 2783.py
Traceback (most recent call last):
  File "2783.py", line 4, in <module>
    draw.textsize("")
  File "/usr/local/lib/python3.6/site-packages/PIL/ImageDraw.py", line 263, in textsize
    return font.getsize(text, direction, features)
  File "/usr/local/lib/python3.6/site-packages/PIL/ImageFont.py", line 111, in getsize
    return self.font.getsize(text)
SystemError: <built-in method getsize of ImagingFont object at 0x7ffee703da00> returned NULL without setting an error

Also fails for draw.textsize("\n") and draw.textsize("test\n").

https://robots.thoughtbot.com/git-bisect

# start up git bisect
git bisect start

# give git a commit/tag where there is not a bug
git bisect good 4.2.0

# give git a commit/tag where there is a bug
git bisect bad 4.3.0

# give git a command to run against each commit
git bisect run ./2783.sh # python3 setup.py develop && python3 2783.py

Confirms fd0e7be55a77dd1d499c0b9df99d24e23e8c15e3 from #2629:

fd0e7be55a77dd1d499c0b9df99d24e23e8c15e3 is the first bad commit
commit fd0e7be55a77dd1d499c0b9df99d24e23e8c15e3
Date:   Tue Jul 18 02:50:40 2017 -0700

    Fix for memory leak in PILfont _font_text_asBytes, #2629

:100644 100644 b7726a2cc4daf8d551f3b4000c522dbd9f2cd44a 5e549a08281cb62c083b131c6d10315e3ff2437c M  _imaging.c
bisect run success

Some validation at the start of ImageDraw.textsize should do it:

    def textsize(self, text, font=None, spacing=4, direction=None,
                 features=None):
        """Get the size of a given string, in pixels."""
        if len(text) == 0:
            return (0, 0)

Since this has been fixed for 2 months, is there a plan for a release in the near future with the fix in it?

@ryanmrubin Yes, Pillow has quarterly releases, the next 4.4.0 is due out on or around 1st January 2018.

Gotcha, thanks!

@ryanmrubin just to be helpful/clear, Pillow 4.4.0 has since been redesignated 5.0.0. So when you see that appear in the next day or so, that is it.

Thanks for the clarification!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

readyready15728 picture readyready15728  Â·  4Comments

naaaargle picture naaaargle  Â·  3Comments

thinrhino picture thinrhino  Â·  3Comments

SysoevDV picture SysoevDV  Â·  3Comments

maxhumber picture maxhumber  Â·  3Comments