【问题标题】:Correctly implementing text wrapping (PIL/Pillow)正确实现文本换行(PIL/Pillow)
【发布时间】:2017-05-03 04:27:23
【问题描述】:

我在黑色条上写文字,然后将所述条粘贴到基础图像上。它运行良好。

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

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

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

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)

【问题讨论】:

    标签: python python-imaging-library pillow


    【解决方案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 会产生次优结果。这不是一个可行的解决方案。

    【讨论】:

      猜你喜欢
      • 2017-09-29
      • 2021-07-07
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      • 2014-04-15
      相关资源
      最近更新 更多