首页 > 解决方案 > Python Wand:如何使文本加粗?

问题描述

如何使用 python wand 库生成粗体文本?我无法让它工作。

http://docs.wand-py.org/en/0.4.1/wand/drawing.html - 关于支持的文档样式:'undefined; 'normal' 'italic' 'oblique' 'any' 有没有粗体样式?

示例用法我想用从内容文件中获得的日期做一个页脚:

        with Image(width=150, height=25,) as img:
            draw.font_family = 'MS Reference Sans Serif'
            draw.font_size = 14.0
            draw.push()
            draw.font_style = 'italics'
            metrics = draw.get_font_metrics(img, contents['date'], multiline=False)
            draw.text(int((img.width - metrics.text_width)/2), int((metrics.text_height)), contents['date'])
            draw.pop()
            draw(img)
            img.save(filename='./temp/footer.png')

也许有一些方法可以让它以某种快速的方式变粗?非常感谢任何帮助。

标签: pythontextpython-imaging-librarywand

解决方案


“粗体”不是一个font_style,而是一个font_weighthttp ://docs.wand-py.org/en/0.4.1/wand/drawing.html#wand.drawing.Drawing.font_weight

它采用的整数值是字体通常使用的值,其中 400 是“正常”,700 是“粗体”。(这些也用于 CSS。参见例如https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Common_weight_name_mapping。)


推荐阅读