Wrap text in PIL

Aim for simple and consistent behavior. The textwrap module, as noted in other answers, only handles fixed-width fonts correctly (it is therefore not a consistent solution). Here is a simple function to wrap your text without breaking words, and it works with variable-width fonts.

from PIL import ImageFont


def get_wrapped_text(text: str, font: ImageFont.ImageFont,
                     line_length: int):
        lines = ['']
        for word in text.split():
            line = f'{lines[-1]} {word}'.strip()
            if font.getlength(line) <= line_length:
                lines[-1] = line
            else:
                lines.append(word)
        return '\n'.join(lines)


if __name__ == '__main__':
    font = ImageFont.truetype('arial.ttf', 12)
    text = 'An example line of text that will need to be wrapped.'
    wrapped_text = get_wrapped_text(text, font, line_length=70)
    # wrapped_text is 'An example\nline of text\nthat will\nneed to be\nwrapped.'

You will need to first split the text into lines of the right length, and then draw each line individually.

The second part is easy, but the first part may be quite tricky to do accurately if varible-width fonts are used. If fixed-width fonts are used, or if accuracy doesn't matter that much, then you can just use the textwrap module to split the text into lines of a given character width:

margin = offset = 40
for line in textwrap.wrap(text, width=40):
    draw.text((margin, offset), line, font=font, fill="#aa0000")
    offset += font.getsize(line)[1]

Well, you can do this manually, of course, using the \n every time you want to wrap the text. It isn't the best way if you have different string everytime but gives entire control over the result. But there is also the textwrap module. You can use it this way:

import textwrap
texto = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
novo = textwrap.wrap(texto, width=20)
print(novo)

Results:

>>> 
['Lorem ipsum dolor', 'sit amet,', 'consectetur', 'adipisicing elit,', 'sed do eiusmod', 'tempor incididunt ut', 'labore et dolore', 'magna aliqua. Ut', 'enim ad minim', 'veniam, quis nostrud', 'exercitation ullamco', 'laboris nisi ut', 'aliquip ex ea', 'commodo consequat.', 'Duis aute irure', 'dolor in', 'reprehenderit in', 'voluptate velit esse', 'cillum dolore eu', 'fugiat nulla', 'pariatur. Excepteur', 'sint occaecat', 'cupidatat non', 'proident, sunt in', 'culpa qui officia', 'deserunt mollit anim', 'id est laborum.']

Returns a list of terms on the previous string wrapped according to the width you determinated.


The accepted solution wraps text basing on the fixed limit of 40 characters per line, not taking into account the box width (in pixels) nor font size. This may easily lead to underfill or overfill.

Here is a better solution - a simple code snippet to wrap text taking into account font-based width measurement: https://gist.github.com/turicas/1455973