正确实现文本换行 (PIL/Pillow)

Correctly implementing text wrapping (PIL/Pillow)

我在黑色条带上写文字,然后将所述条带粘贴到基本图像上。它工作完美。

我的问题是,如果文本有很多字符,它会溢出到基本图像之外。这就是我的意思:

如何修复我的代码,使溢出的文本转到下一行

这是我目前正在做的事情:

background = Image.new('RGBA', (base_width, BACKGROUND_HEIGHT),(0,0,0,128)) #creating the black strip
draw = ImageDraw.Draw(background)
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 16)
text = "Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar"
text_width, text_height = draw.textsize(text,font=font)
position = ((base_width-text_width)/2,(BACKGROUND_HEIGHT-text_height)/2)
draw.text(position,text,(255,255,255),font=font)
offset = (0,base_height/2)
img.paste(background,offset,mask=background)

我最后写了以下内容以确保文本顺畅地排到下一行。如果有人能帮我优化这段相当不优雅的代码,那就太好了:

    base_width, base_height = img.size
    font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", font_size)
    line = ""
    lines = []
    width_of_line = 0
    number_of_lines = 0
    # break string into multi-lines that fit base_width
    for token in text.split():
        token = token+' '
        token_width = font.getsize(token)[0]
        if width_of_line+token_width < base_width:
            line+=token
            width_of_line+=token_width
        else:
            lines.append(line)
            number_of_lines += 1
            width_of_line = 0
            line = ""
            line+=token
            width_of_line+=token_width
    if line:
        lines.append(line)
        number_of_lines += 1
    # create a background strip for the text
    font_height = font.getsize('|')[1]
    background = Image.new('RGBA', (base_width, font_height*number_of_lines),(0,0,0,146)) #creating the black strip
    draw = ImageDraw.Draw(background)
    y_text = 0#h
    # render each sentence 
    for line in lines:
        width, height = font.getsize(line)
        draw.text(((base_width - width) / 2, y_text), line, font=font, fill=fillcolor)
        y_text += height
    # paste the result on the base_image
    offset = (0,base_height/2) #get from user input (e.g. 'top', 'bottom', 'middle')
    img.paste(background,offset,mask=background)

注意:由于 , using Python's textwrap() module 的原因,产生了次优结果。这不是一个可行的解决方案。