python-pillow / python-pillow/Pillow
Multiline text line height not being calculated correctly
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 13.8k
- Forks
- 2.5k
- Avg merge
- 2d 8h
- Merged PRs (30d)
- 89
Description
The line height isn't being calculated correctly for ImageDraw's multiline_text() function [1]. It's making an assumption that a capital "A" is as large as the font can be. However, if I use the word "Apple" you'll notice that the "p" in word creates a different text size.
The issue
Current code
line_spacing = self.textsize('A', font=font)[1] + spacing
>>> self.textsize('A', font=font)[1]
170
>>> self.textsize('APPLE', font=font)[1]
170
>>> self.textsize('Apple', font=font)[1]
244 // <-- problem
How the text correctly looks on the web
How the image is rendered via pillow (incorrect line height)
Proposed Change
import string
...
line_spacing = self.textsize(string.ascii_letters, font=font)[1] + spacing
Gets characters above and below the text baseline. Use every upper and lower letter in the alphabet since you'll notice in the font above, the lower case "l" is the character with the highest point.
When that change is made, you'll see the correct text height in the image:
[1] https://github.com/python-pillow/Pillow/blob/master/PIL/ImageDraw.py#L269
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in PIL/ImageDraw.py at the multiline_text implementation and reproduce the reported textsize results with the example font and strings. Check how line_spacing is derived from glyph bounds, then verify that multiline rendering accounts for both ascenders and descenders without excessive spacing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- computer-graphics
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100