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

3

我正在一个黑色条带上面书写文本,并将该条带粘贴到基础图像上。它的效果非常好。

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

enter image description here

如何修复我的代码,以使溢出的文本进入下一行

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

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)
1个回答

2

我最终写了下面的代码,以确保文本可以舒适地流到下一行。如果有人能帮助我优化这个不太优雅的代码,那将是很棒的:

    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)

注意:出于这里所述的原因,使用Python的textwrap() 模块会产生次优结果。这不是一个可行的解决方案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接