【问题标题】:python wand: change text style with draw.text()python wand:使用 draw.text() 更改文本样式
【发布时间】:2015-06-02 02:39:33
【问题描述】:

我正在使用draw.text() 在画布上绘制一些文本。但是该函数似乎只接受 3 个参数 x、y 和 body,因此无法指定什么字体、颜色等。可能我在这里遗漏了一些东西,因为这是非常基本的功能。我错过了什么?

【问题讨论】:

    标签: python imagemagick wand


    【解决方案1】:

    使用wand.drawing.Drawing,您需要构建绘图对象的“上下文”。可以通过直接在draw 对象实例上设置属性来定义字体样式、系列、粗细、颜色等等。

    from wand.image import Image
    from wand.color import Color
    from wand.drawing import Drawing
    from wand.display import display
    
    with Image(width=200, height=150, background=Color('lightblue')) as canvas:
        with Drawing() as context:
            context.fill_color = Color('orange')
            context.stroke_color = Color('brown')
            context.font_style = 'italic'
            context.font_size = 24
            context.text(x=25,
                         y=75,
                         body="Hello World!")
            context(canvas)
            canvas.format = "png"
            display(canvas)
    

    但是如果您的draw 对象已经具有矢量属性怎么办?

    这是 Drawing.push()Drawing.pop() 可用于管理您的绘图堆栈的地方。

     # Default attributes for drawing circles
     context.fill_color = Color('lime')
     context.stroke_color = Color('green')
     context.arc((75, 75), (25, 25), (0, 360))
     # Grow context stack for text style attributes
     context.push()
     context.fill_color = Color('orange')
     context.stroke_color = Color('brown')
     context.font_style = 'italic'
     context.font_size = 24
     context.text(x=25,
                 y=75,
                 body="Hello World!")
     # Return to previous style attributes
     context.pop()
     context.arc((175, 125), (150, 100), (0, 360))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多