Drawing

New in version 0.3.0.

The wand.drawing module provides some basic drawing functions. wand.drawing.Drawing object buffers instructions for drawing shapes into images, and then it can draw these shapes into zero or more images.

It’s also callable and takes an Image object:

from wand.drawing import Drawing
from wand.image import Image

with Drawing() as draw:
    # does something with ``draw`` object,
    # and then...
    with Image(filename='wandtests/assets/beach.jpg') as image:
        draw(image)

Lines

You can draw lines using line() method. It simply takes two (x, y) coordinates for start and end of a line. For example, the following code draws a diagonal line into the image:

draw.line((0, 0), image.size)
draw(image)

Or you can turn this diagonal line upside down:

draw.line((0, image.height), (image.width, 0))
draw(image)

The line color is determined by fill_color property, and you can change this of course. The following code draws a red diagonal line into the image:

from wand.color import Color

with Color('red') as color:
    draw.fill_color = color
    draw.line((0, 0), image.size)
    draw(image)

Rectangles

New in version 0.3.6.

If you want to draw rectangles use rectangle() method. It takes left/top coordinate, and right/bottom coordinate, or width and height. For example, the following code draws a square on the image:

draw.rectangle(left=10, top=10, right=40, bottom=40)
draw(image)

Or using width and height instead of right and bottom:

draw.rectangle(left=10, top=10, width=30, height=30)
draw(image)

Note that the stoke and the fill are determined by the following properties:

Texts

Drawing object can write texts as well using its text() method. It takes x and y cordinates to be drawn and a string to write:

draw.font = 'wandtests/assets/League_Gothic.otf'
draw.font_size = 40
draw.text(image.width / 2, image.height / 2, 'Hello, world!')
draw(image)

As the above code shows you can adjust several settings before writing texts:

Table Of Contents

Related Topics

This Page