-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Description
What did you do?
I'm using getsize_multiline to compute the size of a text label that I render to an image. The image is exactly the size of the text label.
I noticed that my text labels where cropped; specifically, when there are letters like p or g (which extend below the baseline) on the last line of text, these letters get cropped.
The problem doesn't affect getsize, and can easily be exhibited by comparing the sizes reported by getsize vs getsize_multiline for a single line string like g.
Example:
from PIL import ImageFont
font = ImageFont.truetype("DroidSans", 20)
print(font.getsize("g"))
print(font.getsize_multiline("g"))What did you expect to happen?
The getsize and getsize_multiline methods should return the same size.
What actually happened?
With DroidSans in size 20:
getsizereturns (10,24)getsize_multilinereturns (10,19)
The g actually extends below if I create a label of size 10,19 and draw the string g on it, the g gets cropped.
What are your OS, Python and Pillow versions?
- OS: Arch Linux
- Python: 3.9
- Pillow: 8.4.0
#!/usr/bin/env python
from PIL import Image, ImageFont, ImageDraw
font = ImageFont.truetype("DroidSans", 20)
text = "gÂp"
size = font.getsize(text)
image = Image.new("RGB", size)
draw = ImageDraw.Draw(image)
draw.text((0, 0), text, font=font)
image.save("getsize.png")
size = font.getsize_multiline(text)
image = Image.new("RGB", size)
draw = ImageDraw.Draw(image)
draw.text((0, 0), text, font=font)
image.save("getsize_multiline.png")I imagine that the logic in getsize_multiline doesn't take into accounts characters that extend below the baseline. For now I'm going to just add a bit of padding on the last line 😅 but I guess there has to be a better way.

