Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

getsize() is throwing "{OSError}too many function definitions" for certain fonts

Open norahsakal opened this issue 5 years ago • 4 comments

What did you do?

I tried to use a font called Singo and it works to read the font ImageFont.truetype(font="singo.ttf", size=14) but then I'm getting the error {OSError}too many function definitions when I try to get the text size. It is crashing in the function getsize() in the file ImageFont.py

I tried with two different font files, one of them works even though they look identical on my computer and I am able to install both on my computer. For reference, here are the two fonts; https://github.com/norahsakal/test-font-debugging

What did you expect to happen?

I expected to get the height and width from the font.

What actually happened?

The function getsize() in the file ImageFont.py is throwing the error {OSError}too many function definitions

What are your OS, Python and Pillow versions?

  • OS: MacOS
  • Python: 3.7
  • Pillow: 8.2.0

Here is what I tried to do

from PIL import Image, ImageDraw
import PIL.ImageFont as ImageFont

draw = ImageDraw.Draw('image.pg')
font_text = ImageFont.truetype("singo.ttf", 13)
text = "Hello"
w, h = draw.textsize(text, font=font_text)

The error

File "/tmp/sls-py-req/PIL/ImageDraw.py", line 569, in textsize
return font.getsize(text, direction, features, language, stroke_width)
File "/tmp/sls-py-req/PIL/ImageFont.py", line 430, in getsize
size, offset = self.font.getsize(text, "L", direction, features, language)
OSError: too many function definitions

norahsakal avatar May 06 '21 06:05 norahsakal

"too many function definitions" is actually a FreeType error - https://www.freetype.org/freetype2/docs/reference/ft2-error_code_values.html

I expect that the font is simply broken.

radarhere avatar May 06 '21 08:05 radarhere

@norahsakal do you disagree?

radarhere avatar May 07 '21 23:05 radarhere

Thank you so much for your prompt reply @radarhere, I truly appreciate it! Closing this 🙏

norahsakal avatar May 08 '21 01:05 norahsakal

I had a similar issue and found that it was a broken font (ttf) file. I ended up finding an OTF version of the file and this helped.

kfordaccela avatar Jan 29 '22 00:01 kfordaccela