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

draw text position incorrect #2486

Closed
3lixy opened this issue Apr 12, 2017 · 5 comments
Closed

draw text position incorrect #2486

3lixy opened this issue Apr 12, 2017 · 5 comments

Comments

@3lixy
Copy link

3lixy commented Apr 12, 2017

What did you do?

Try and place text in the top corner of the image

What did you expect to happen?

Text is positioned at the top corner of the image

What actually happened?

The text is indent by a few positive pixels on x axis and around 10 positive pixels on the y axis.

test

What versions of Pillow and Python are you using?

$ python -V
Python 2.7.9
$ pip freeze | grep -i pillow
Pillow==4.1.0
$ uname -a
Linux revo-vpn-74 3.16.0-4-amd64 #1 SMP Debian 3.16.39-1+deb8u1 (2017-02-22) x86_64 GNU/Linux
$ cat /etc/*release*
PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
NAME="Debian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=debian
HOME_URL="http://www.debian.org/"
SUPPORT_URL="http://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Please include code that reproduces the issue and whenever possible, an image that demonstrates the issue. The best reproductions are self-contained scripts with minimal dependencies. If you are using a framework such as plone, django, or buildout, try to replicate the issue just using Pillow.

# -*- coding: utf-8 -*-
from __future__ import unicode_literals, division, absolute_import, print_function
from PIL import Image, ImageDraw, ImageFont
img = Image.new('RGBA', (250,50), color=(150,150,150,255))
font = ImageFont.truetype('arial.ttf', 50)
draw = ImageDraw.Draw(img)
draw.text((0,0), 'TEXT', font=font)
img.save('/tmp/test.png')

arial.ttf md5sum 096b6677fc4a0a2ab430cd3d28b6fd52

@hugovk
Copy link
Member

hugovk commented Apr 12, 2017

The font has a vertical offset:

print(font.getoffset('TEXT'))  # (0, 9)
draw.text((0, -9), 'TEXT', font=font)
img.save('/tmp/test2.png')

Produces:
test2.png

@3lixy
Copy link
Author

3lixy commented Apr 12, 2017

ok i thought it might be something like that but could not find a definitive answer.

Do you know of a way/point me in the right direction to calculate/discover what this offset will be?

@hugovk
Copy link
Member

hugovk commented Apr 12, 2017

Here's one way:

text = 'TEXT'
offset = font.getoffset(text)
draw.text((0-offset[0], 0-offset[1]), text, font=font)
img.save('/tmp/test3.png')

@3lixy
Copy link
Author

3lixy commented Apr 12, 2017

oh ok note to self: RTFM
thanks

@3lixy 3lixy closed this as completed Apr 12, 2017
@stephahn
Copy link

stephahn commented Dec 6, 2019

Hello
did you manage to correct the offset on the x-axis? Because it is always equal to 0 for me, but in practice, there is always a nonnull offset.

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

No branches or pull requests

3 participants