Skip to content

Multiline text line height not being calculated correctlyΒ #1646

@troygrosfield

Description

@troygrosfield

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

screen shot 2016-01-05 at 12 13 35 pm

How the image is rendered via pillow (incorrect line height)

screen shot 2016-01-05 at 12 13 49 pm

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:

screen shot 2016-01-05 at 12 50 51 pm

[1] https://github.com/python-pillow/Pillow/blob/master/PIL/ImageDraw.py#L269

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugAny unexpected behavior, until confirmed feature.Conversion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions