镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

ImageDraw.textsize() returns incorrect textsize for truetype fonts #644

Closed

Description

@tk0miya

ImageDraw.textsize() does not return correct textsize for truetype fonts.
The return value does not include offset.

So I have to adjust offset using FreeTypeFont.getoffset() to get correct textsize.

from PIL import Image, ImageDraw, ImageFont

image = Image.new('RGB', (512, 256), (255, 255, 255))
drawer = ImageDraw.Draw(image)
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeSerif.ttf', 32)

# drawing text
STRING = 'Hello, python language!'
drawer.text((10, 10), STRING, fill='black', font=font)

# drawing rectangle surrounding text
size = drawer.textsize(STRING, font=font)
offset = font.getoffset(STRING)
drawer.rectangle((10, 10, 10 + size[0] + offset[0], 10 + size[1] + offset[1]), outline='black')

image.save('example.png', 'PNG')

is this correct way?

And, I think ImageDraw.textsize() should return textsize including offset.

Activity

  1. added this to the Future milestone on Jun 7, 2014
  2. fepegar commented on Nov 23, 2019

    @fepegar
  3. aschenkuttel commented on Feb 28, 2020

    @aschenkuttel

    Had to do a hacky workaround aswell

  4. radarhere commented on Feb 17, 2024

    @radarhere
    Member

    ImageDraw.textsize() was been removed in Pillow 10.0.0. See https://pillow.readthedocs.io/en/stable/deprecations.html#font-size-and-offset-methods for more information.

    If you still have a problem, please open a new issue so that it can be discussed further.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugAny unexpected behavior, until confirmed feature.PNG

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions