Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用PIL画下划线文本_Python_Python Imaging Library_Underline - Fatal编程技术网

Python 用PIL画下划线文本

Python 用PIL画下划线文本,python,python-imaging-library,underline,Python,Python Imaging Library,Underline,有一篇帖子与粗体/斜体相关: 但是,如何使用PIL绘制下划线文本 看起来没有标准的方法来实现这一点,但您始终可以实现它 可能的解决办法: import Image import ImageDraw import ImageFont def draw_underlined_text(draw, pos, text, font, **options): twidth, theight = draw.textsize(text, font=font) lx, ly = po

有一篇帖子与粗体/斜体相关:


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

看起来没有标准的方法来实现这一点,但您始终可以实现它

可能的解决办法:

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()