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

I am using draw.text()

to draw text on canvas. But the function apparently takes 3 parameters x, y and body, so there is no way to specify which font, color, etc. I probably missed something because this is a pretty basic functionality. What am I missing?

+3


source to share


1 answer


With wand.drawing.Drawing

you need to build the "context" of the drawing object. Font style, family, weight, color, and more can be defined by setting attributes directly on an object instance 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)

      

change text style with wand.draw

But what if your object draw

already has vector attributes?



Here Drawing.push()

and Drawing.pop()

can be used to manage the stack of pictures.

 # 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

+5


source







All Articles