【问题标题】:Draw underline text with PIL使用 PIL 绘制下划线文本
【发布时间】:2012-02-10 17:14:44
【问题描述】:

有一篇与粗体/斜体相关的帖子: Draw bold/italic text with PIL?

但是,如何使用 PIL 绘制下划线文本?

【问题讨论】:

    标签: python python-imaging-library underline


    【解决方案1】:

    看起来没有标准的方法来做到这一点,但你总是可以实现它。

    可能的解决方案:

    import Image
    import ImageDraw
    import ImageFont
    
    def draw_underlined_text(draw, pos, text, font, **options):    
        twidth, theight = draw.textsize(text, font=font)
        lx, ly = pos[0], pos[1] + theight
        draw.text(pos, text, font=font, **options)
        draw.line((lx, ly, lx + twidth, ly), **options)
    
    im = Image.new('RGB', (400, 400), (255,)*3)
    draw = ImageDraw.Draw(im)
    font = ImageFont.truetype("arial.ttf", 50)
    
    draw_underlined_text(draw, (50, 150), 'Hello PIL!', font, fill=0)
    draw_underlined_text(draw, (50, 300), 'Test', font, fill=128)
    
    im.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多