Skip to content

Example code in ImageText documentation makes incorrect use of get_bbox() #10583

In review

Description

@samhocevar

The documentation for ImageText uses the following code:

im = Image.new("RGB", text.get_bbox()[2:])
d = ImageDraw.Draw(im)
d.text((0, 0), text, "#f00")

It will create an image of the wrong size because it will not subtract the left and top values of the bounding box. Furthermore, it will make it impossible to adjust the position of im relative to a different anchor without, again, subtracting the left and top values of the default bbox.

Activity

  1. radarhere commented on Nov 22, 2026

    @radarhere
    Member

    If I take that whole example from the docs, and save the image at the end,

    from PIL import Image, ImageDraw, ImageFont, ImageText
    font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 24)
    
    text = ImageText.Text("Hello world", font)
    text.embed_color()
    text.stroke(2, "#0f0")
    
    print(text.get_length())  # 154.0
    print(text.get_bbox())  # (-2, 3, 156, 22)
    
    im = Image.new("RGB", text.get_bbox()[2:])
    d = ImageDraw.Draw(im)
    d.text((0, 0), text, "#f00")
    im.save("out.png")

    I get
    out

    You can see that the image size contains all of the text when it is drawn at (0, 0) (GitHub is applying some border-radius styling, but hopefully you get the point).

    You were expecting the text not to have space to the top and left of the text. That's possible with Pillow, but I don't know that it needs to be part of this specific example? I don't think everyone necessarily wants to draw text without that spacing.

    The three lines that you have highlighted are intended to show that ImageText's getbbox() can get the bounding box without an ImageDraw instance. It achieves that.

  2. samhocevar commented on Nov 23, 2026

    @samhocevar
    Author

    Well thank you for the example, it will make it easier to explain and to understand.

    If you just zoom on the picture in your example, you will see that:

    • there are pixels missing on the left (because the value -2 in the bounding box is ignored)
    • the image is too large in the vertical dimension (because the value 3 in the bounding box is ignored)
    image

    You have to create an image of the proper dimensions, using the actual bounding box size:

    im = Image.new("RGB", (text.get_bbox()[2] - text.get_bbox()[0],
                           text.get_bbox()[3] - text.get_bbox()[1]))

    And then you have to write the text at the proper coordinates:

    d.text((-text.get_bbox()[0], -text.get_bbox()[1]), text, "#f00")

    This fixes both problems:

    image
  3. samhocevar commented on Nov 23, 2026

    @samhocevar
    Author

    FYI, it gets more obvious with a huge stroke size, such as 6:

    image

    Fixed:

    image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions