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.textsize() returns incorrect textsize for truetype fonts #644

Closed
tk0miya opened this issue Apr 30, 2014 · 3 comments · Fixed by #784
Closed

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

tk0miya opened this issue Apr 30, 2014 · 3 comments · Fixed by #784
Labels
Bug Any unexpected behavior, until confirmed feature.
Milestone

Comments

@tk0miya
Copy link
Contributor

tk0miya commented Apr 30, 2014

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.

@fepegar
Copy link

fepegar commented Nov 23, 2019

This seems to keep happening: https://stackoverflow.com/questions/59008322/pillow-imagedraw-text-coordinates-to-center/59008967#59008967

@aschenkuttel
Copy link

Had to do a hacky workaround aswell

@radarhere
Copy link
Member

radarhere commented Feb 17, 2024

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
Labels
Bug Any unexpected behavior, until confirmed feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants