【问题标题】:Is it possible to use multiple fonts in Python Wand (Drawing Caption)?是否可以在 Python Wand(绘图标题)中使用多种字体?
【发布时间】:2020-10-27 20:41:11
【问题描述】:

我在 Wand 中使用以下内容

 with Image(width=width, height=height) as canvas:
        left, top, width, height = 53, 247, 918, 78
        with Drawing() as context:
            context.fill_color = 'transparent'
            context.rectangle(left=left, top=top, width=width, height=height)
            font = Font('Montserrat-Regular.ttf', 0, "white")
            context(canvas)
            canvas.caption(row['DetailText'],
                           left=left, top=top, width=width, height=height, font=font, gravity='center')
        canvas.save(filename='result.png')

效果很好。我想在 row['DetailText'] 中的文本前面加上“NOTES”一词,并以粗体显示。有没有办法做到这一点?

【问题讨论】:

    标签: python wand


    【解决方案1】:

    是的,您可以在 Wand 的绘图上下文中定义多种字体。需要进行一些规划,因为您必须将每个文本的样式彼此隔离。使用Drawing.get_font_metrics()方法记录将要渲染的文本的尺寸,并对剩余的文本进行偏移。

    with Image(width=300, height=100, pseudo='plasma:') as img:
        with Drawing() as ctx:
            # Global property shared between two styles.
            ctx.font_size = 25
            # Isolate single style.
            ctx.push()
            ctx.font = 'STIX-Bold-Italic'
            ctx.fill_color = 'orange'
            # Grab metrics about the rendered text.
            font_metrics = ctx.get_font_metrics(img, 'Hello ')
            # Draw first "bold" part.
            ctx.text(50, 75, 'Hello')
            ctx.pop()
            # Create another isolated style context.
            ctx.push()
            ctx.font = 'UMTypewriter'
            ctx.fill_color = 'red'
            ctx.text(50 + int(font_metrics.text_width), 75, 'world')
            ctx.pop()
            # Render context.
            ctx(img)
        img.save(filename='output.png')
    

    【讨论】:

    • 谢谢。使用 .caption 而不是 .text 这对我有用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 2020-02-06
    • 2021-12-29
    • 2015-08-25
    • 2015-07-19
    • 1970-01-01
    相关资源
    最近更新 更多