Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageDraw.textbbox doesn't work with the default font #5506

Closed
iguessthislldo opened this issue May 23, 2021 · 3 comments · Fixed by #5510
Closed

ImageDraw.textbbox doesn't work with the default font #5506

iguessthislldo opened this issue May 23, 2021 · 3 comments · Fixed by #5510

Comments

@iguessthislldo
Copy link

What did you do?

I tried to use ImageDraw.textbbox without specifying a font.

What did you expect to happen?

I expected ImageDraw.textbbox to work without specifying a font like ImageDraw.text does.

What actually happened?

It throws an AttributeError:

File "<...>/PIL/ImageDraw.py", line 657, in textbbox
    bbox = font.getbbox(
AttributeError: 'ImageFont' object has no attribute 'getbbox'

It also does this when specifying ImageFont.load_default() manually as shown below. It does work when using another font, as shown below in the commented out line.

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 18.04
  • Python: 3.6.9
  • Pillow: 8.2.0
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO

size = (256, 256)

def thumbnail(orig, fmt, text=None):
    if isinstance(orig, bytes):
        orig = BytesIO(orig)
    image = Image.open(orig)
    image.thumbnail(size)
    if text is not None:
        draw = ImageDraw.Draw(image)
        # font = ImageFont.truetype("FreeSans", size=20)
        font = ImageFont.load_default()
        print(draw.textbbox((0, 0), text, font=font))
        draw.text((0, 0), text, font=font)
    result = BytesIO()
    image.save(result, fmt)
    return result.getvalue()
@radarhere radarhere changed the title ImageDraw.textbbox Doesn't Work With the Default Font ImageDraw.textbbox doesn't work with the default font May 24, 2021
@nulano
Copy link
Contributor

nulano commented May 24, 2021

From the docs (emphasis added) https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.ImageDraw.textbbox:

Returns bounding box (in pixels) of given text relative to given anchor when rendered in font with provided direction, features, and language. Only supported for TrueType fonts.

The default ImageDraw font is in PIL font format, not TrueType.

The default font also doesn't support anchors and all glyphs fit within their own bounding box, so textbbox offers no advantages over textsize. (The inverse is not true, textsize is unreliable for TrueType fonts.) As a workaround you can use the following:

if hasattr(font, "getbbox"):
    bbox = draw.textbbox(...)
else:
    w, h = draw.textsize(...)
    bbox = (x, y, x + w, y + h)

@iguessthislldo
Copy link
Author

Alright, thanks I missed that it only worked for TrueType fonts. It would be nice if there was a check for that in textbbox so the mistake made was clear.

@radarhere
Copy link
Member

I've created #5510 to add a specific error for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants