Python图像库中的删除线文本

时间:2015-07-09 10:52:43

标签: python fonts python-imaging-library strikethrough

有没有办法使用Python Image Library的ImageFont模块制作删除线文本?

my_font = ImageFont.truetype("Trebuchet MS Bold.ttf", 13)
# DO SOMETHING HERE TO MAKE FONT STRIKETHROUGH?
draw.text((x,y), text, fill=(16, 31, 83), font=my_font)

提到的解决方案here不起作用。下面的代码然后生成以下图像。

enter image description here

from PIL import Image, ImageDraw, ImageFont

def draw_strikethrough_text():
    hello = 'Hello '
    cruel = 'cruel'
    world = ' world'

    cruel = strikethrough(cruel)

    img = Image.new('RGBA', (200, 100), "white")
    draw = ImageDraw.Draw(img)
    hw_font = ImageFont.truetype("Trebuchet MS.ttf", 13)

    textsize_hello = draw.textsize(hello, hw_font)[0]
    textsize_cruel = draw.textsize(cruel, hw_font)[0]

    margin = 20
    x_hello = margin
    x_cruel = x_hello + textsize_hello
    x_world = x_cruel + textsize_cruel
    y = margin

    draw.text((x_hello, y), hello, fill=(60, 60, 60), font=hw_font)
    draw.text((x_cruel, y), cruel, fill=(60, 60, 60), font=hw_font)
    draw.text((x_world, y), world, fill=(60, 60, 60), font=hw_font)

    img.save('img/hello_cruel_world.png', quality=95, optimize=True)

def strikethrough(text):
    return '\u0336'.join(text) + '\u0336'

if __name__ == '__main__':
    draw_strikethrough_text()

0 个答案:

没有答案