正确实施文字包装(PIL / Pillow)

时间:2017-05-03 04:27:23

标签: python python-imaging-library pillow

我在黑色条带上书写文字,然后在基本图像上粘贴所述条带。它工作得很好。

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

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 个答案:

答案 0 :(得分:1)

我最后编写了以下内容以确保文本可以轻松地流向下一行。如果有人可以帮助我优化这个相当不优雅的代码,那就太棒了:

    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)

注意:出于explained here的原因,使用Python textwrap() module会产生次优结果。它不是一个可行的解决方案。