lang2
lang2

Reputation: 11966

python wand: change text style with draw.text()

I'm using draw.text() to draw some text on a canvas. But the function only seem to take 3 parameters x, y, and body so there is no way to specify what font, color, etc. Probably I'm missing something here because this is pretty basic functionality. What am I missing?

Upvotes: 4

Views: 4476

Answers (1)

emcconville
emcconville

Reputation: 24419

With wand.drawing.Drawing, you need to build the "context" of the drawing object. Font style, family, weight, color, and much more, can be defined by setting attributes directly on the draw object instance.

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)

change text style with wand.draw

But what if your draw object already has vector attributes?

This is where Drawing.push() & Drawing.pop() can be used to manage your drawing stack.

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

text style with context-stack

Upvotes: 10

Related Questions