5

有一篇与粗体/斜体相关的帖子: 用 PIL 绘制粗体/斜体文本?

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

4

1 回答 1

7

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

可能的解决方案:

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()
于 2012-01-15T09:31:02.557 回答