wand.drawing — Drawings

The module provides some vector drawing functions.

New in version 0.3.0.

wand.drawing.CLIP_PATH_UNITS = ('undefined_path_units', 'user_space', 'user_space_on_use', 'object_bounding_box')

(collections.abc.Sequence) The list of clip path units

  • 'undefined_path_units'
  • 'user_space'
  • 'user_space_on_use'
  • 'object_bounding_box'
wand.drawing.FILL_RULE_TYPES = ('undefined', 'evenodd', 'nonzero')

(collections.abc.Sequence) The list of fill-rule types.

  • 'undefined'
  • 'evenodd'
  • 'nonzero'
wand.drawing.FONT_METRICS_ATTRIBUTES = ('character_width', 'character_height', 'ascender', 'descender', 'text_width', 'text_height', 'maximum_horizontal_advance', 'x1', 'y1', 'x2', 'y2', 'x', 'y')

(collections.abc.Sequence) The attribute names of font metrics.

wand.drawing.GRAVITY_TYPES = ('forget', 'north_west', 'north', 'north_east', 'west', 'center', 'east', 'south_west', 'south', 'south_east', 'static')

(collections.abc.Sequence) The list of text gravity types.

  • 'forget'
  • 'north_west'
  • 'north'
  • 'north_east'
  • 'west'
  • 'center'
  • 'east'
  • 'south_west'
  • 'south'
  • 'south_east'
  • 'static'
wand.drawing.LINE_CAP_TYPES = ('undefined', 'butt', 'round', 'square')

(collections.abc.Sequence) The list of LineCap types

  • 'undefined;
  • 'butt'
  • 'round'
  • 'square'
wand.drawing.LINE_JOIN_TYPES = ('undefined', 'miter', 'round', 'bevel')

(collections.abc.Sequence) The list of LineJoin types

  • 'undefined'
  • 'miter'
  • 'round'
  • 'bevel'
wand.drawing.PAINT_METHOD_TYPES = ('undefined', 'point', 'replace', 'floodfill', 'filltoborder', 'reset')

(collections.abc.Sequence) The list of paint method types.

  • 'undefined'
  • 'point'
  • 'replace'
  • 'floodfill'
  • 'filltoborder'
  • 'reset'
wand.drawing.STRETCH_TYPES = ('undefined', 'normal', 'ultra_condensed', 'extra_condensed', 'condensed', 'semi_condensed', 'semi_expanded', 'expanded', 'extra_expanded', 'ultra_expanded', 'any')

(collections.abc.Sequence) The list of stretch types for fonts

  • 'undefined;
  • 'normal'
  • 'ultra_condensed'
  • 'extra_condensed'
  • 'condensed'
  • 'semi_condensed'
  • 'semi_expanded'
  • 'expanded'
  • 'extra_expanded'
  • 'ultra_expanded'
  • 'any'
wand.drawing.STYLE_TYPES = ('undefined', 'normal', 'italic', 'oblique', 'any')

(collections.abc.Sequence) The list of style types for fonts

  • 'undefined;
  • 'normal'
  • 'italic'
  • 'oblique'
  • 'any'
wand.drawing.TEXT_ALIGN_TYPES = ('undefined', 'left', 'center', 'right')

(collections.abc.Sequence) The list of text align types.

  • 'undefined'
  • 'left'
  • 'center'
  • 'right'
wand.drawing.TEXT_DECORATION_TYPES = ('undefined', 'no', 'underline', 'overline', 'line_through')

(collections.abc.Sequence) The list of text decoration types.

  • 'undefined'
  • 'no'
  • 'underline'
  • 'overline'
  • 'line_through'
wand.drawing.TEXT_DIRECTION_TYPES = ('undefined', 'right_to_left', 'left_to_right')

(collections.abc.Sequence) The list of text direction types.

  • 'undefined'
  • 'right_to_left'
  • 'left_to_right'
class wand.drawing.Drawing(drawing=None)

Drawing object. It maintains several vector drawing instructions and can get drawn into zero or more Image objects by calling it.

For example, the following code draws a diagonal line to the image:

with Drawing() as draw:
    draw.line((0, 0), image.size)
    draw(image)
Parameters:drawing (Drawing) – an optional drawing object to clone. use clone() method rather than this parameter

New in version 0.3.0.

affine(matrix)

Adjusts the current affine transformation matrix with the specified affine transformation matrix. Note that the current affine transform is adjusted rather than replaced.

                                  | sx  rx  0 |
| x', y', 1 |  =  | x, y, 1 |  *  | ry  sy  0 |
                                  | tx  ty  1 |
Parameters:matrix (collections.abc.Sequence) – a list of Real to define affine matrix [sx, rx, ry, sy, tx, ty]

New in version 0.4.0.

alpha(x=None, y=None, paint_method='undefined')

Paints on the image’s opacity channel in order to set effected pixels to transparent.

To influence the opacity of pixels. The available methods are:
  • 'undefined'
  • 'point'
  • 'replace'
  • 'floodfill'
  • 'filltoborder'
  • 'reset'

Note

This method replaces matte() in ImageMagick version 7. An AttributeError will be raised if attempting to call on a library without DrawAlpha support.

New in version 0.5.0.

arc(start, end, degree)

Draws a arc using the current stroke_color, stroke_width, and fill_color.

Parameters:

New in version 0.4.0.

bezier(points=None)

Draws a bezier curve through a set of points on the image, using the specified array of coordinates.

At least four points should be given to complete a bezier path. The first & forth point being the start & end point, and the second & third point controlling the direction & curve.

Example bezier on image

with Drawing() as draw:
    points = [(40,10), # Start point
              (20,50), # First control
              (90,10), # Second control
              (70,40)] # End point
    draw.stroke_color = Color('#000')
    draw.fill_color = Color('#fff')
    draw.bezier(points)
    draw.draw(image)
Parameters:points (list) – list of x,y tuples

New in version 0.4.0.

border_color

(Color) the current border color. It also can be set. This attribute controls the behavior of color() during 'filltoborder' operation.

New in version 0.4.0.

circle(origin, perimeter)

Draws a circle from origin to perimeter

Parameters:

New in version 0.4.0.

clip_path

(basestring) The current clip path. It also can be set.

New in version 0.4.0.

clip_rule

(basestring) The current clip rule. It also can be set. It’s a string value from FILL_RULE_TYPES list.

New in version 0.4.0.

clip_units

(basestring) The current clip units. It also can be set. It’s a string value from CLIP_PATH_UNITS list.

New in version 0.4.0.

clone()

Copies a drawing object.

Returns:a duplication
Return type:Drawing
color(x=None, y=None, paint_method='undefined')

Draws a color on the image using current fill color, starting at specified position & method.

Available methods in wand.drawing.PAINT_METHOD_TYPES:

  • 'undefined'
  • 'point'
  • 'replace'
  • 'floodfill'
  • 'filltoborder'
  • 'reset'

New in version 0.4.0.

comment(message=None)

Adds a comment to the vector stream.

Parameters:message (basestring) – the comment to set.

New in version 0.4.0.

composite(operator, left, top, width, height, image)

Composites an image onto the current image, using the specified composition operator, specified position, and at the specified size.

Parameters:
  • operator – the operator that affects how the composite is applied to the image. available values can be found in the COMPOSITE_OPERATORS list
  • typeCOMPOSITE_OPERATORS
  • left (numbers.Real) – the column offset of the composited drawing source
  • top (numbers.Real) – the row offset of the composited drawing source
  • width (numbers.Real) – the total columns to include in the composited source
  • height (numbers.Real) – the total rows to include in the composited source

New in version 0.4.0.

draw(image)

Renders the current drawing into the image. You can simply call Drawing instance rather than calling this method. That means the following code which calls Drawing object itself:

drawing(image)

is equivalent to the following code which calls draw() method:

drawing.draw(image)
Parameters:image (BaseImage) – the image to be drawn
ellipse(origin, radius, rotation=(0, 360))

Draws a ellipse at origin with independent x & y radius. Ellipse can be partial by setting start & end rotation.

Parameters:

New in version 0.4.0.

fill_color

(Color) The current color to fill. It also can be set.

fill_opacity

(Real) The current fill opacity. It also can be set.

New in version 0.4.0.

fill_rule

(basestring) The current fill rule. It can also be set. It’s a string value from FILL_RULE_TYPES list.

New in version 0.4.0.

font

(basestring) The current font name. It also can be set.

font_family

(basestring) The current font family. It also can be set.

New in version 0.4.0.

font_resolution

(Sequence) The current font resolution. It also can be set.

New in version 0.4.0.

font_size

(numbers.Real) The font size. It also can be set.

font_stretch

(basestring) The current font stretch variation. It also can be set, but will only apply if the font-family or encoder supports the stretch type.

New in version 0.4.0.

font_style

(basestring) The current font style. It also can be set, but will only apply if the font-family supports the style.

New in version 0.4.0.

font_weight

(Integral) The current font weight. It also can be set.

New in version 0.4.0.

get_font_metrics(image, text, multiline=False)

Queries font metrics from the given text.

Parameters:
  • image (BaseImage) – the image to be drawn
  • text (basestring) – the text string for get font metrics.
  • multiline (boolean) – text is multiline or not
gravity

(basestring) The text placement gravity used when annotating with text. It’s a string from GRAVITY_TYPES list. It also can be set.

line(start, end)

Draws a line start to end.

Parameters:
matte(x=None, y=None, paint_method='undefined')

Paints on the image’s opacity channel in order to set effected pixels to transparent.

To influence the opacity of pixels. The available methods are:
  • 'undefined'
  • 'point'
  • 'replace'
  • 'floodfill'
  • 'filltoborder'
  • 'reset'

Note

This method has been replace by alpha() in ImageMagick version 7. An AttributeError will be raised if attempting to call on a library without DrawMatte support.

New in version 0.4.0.

opacity

(Real) returns the opacity used when drawing with the fill or stroke color or texture. Fully opaque is 1.0. This method only affects vector graphics, and is experimental. To set the opacity of a drawing, use Drawing.fill_opacity & Drawing.stroke_opacity

New in version 0.4.0.

path_close()

Adds a path element to the current path which closes the current subpath by drawing a straight line from the current point to the current subpath’s most recent starting point.

New in version 0.4.0.

path_curve(to=None, controls=None, smooth=False, relative=False)

Draws a cubic Bezier curve from the current point to given to (x,y) coordinate using controls points at the beginning and the end of the curve. If smooth is set to True, only one controls is expected and the previous control is used, else two pair of coordinates are expected to define the control points. The to coordinate then becomes the new current point.

Parameters:

New in version 0.4.0.

path_curve_to_quadratic_bezier(to=None, control=None, smooth=False, relative=False)

Draws a quadratic Bezier curve from the current point to given to coordinate. The control point is assumed to be the reflection of the control point on the previous command if smooth is True, else a pair of control coordinates must be given. Each coordinates can be relative, or absolute, to the current point by setting the relative flag. The to coordinate then becomes the new current point, and the control coordinate will be assumed when called again when smooth is set to true.

Parameters:

New in version 0.4.0.

path_elliptic_arc(to=None, radius=None, rotation=0.0, large_arc=False, clockwise=False, relative=False)

Draws an elliptical arc from the current point to given to coordinates. The to coordinates can be relative, or absolute, to the current point by setting the relative flag. The size and orientation of the ellipse are defined by two radii (rx, ry) in radius and an rotation parameters, which indicates how the ellipse as a whole is rotated relative to the current coordinate system. The center of the ellipse is calculated automagically to satisfy the constraints imposed by the other parameters. large_arc and clockwise contribute to the automatic calculations and help determine how the arc is drawn. If large_arc is True then draw the larger of the available arcs. If clockwise is true, then draw the arc matching a clock-wise rotation.

Parameters:

New in version 0.4.0.

path_finish()

Terminates the current path.

New in version 0.4.0.

path_horizontal_line(x=None, relative=False)

Draws a horizontal line path from the current point to the target point. Given x parameter can be relative, or absolute, to the current point by setting the relative flag. The target point then becomes the new current point.

Parameters:
  • x (Real) – Real x-axis point to draw to.
  • relative (bool) – bool treat given point as relative to current point

New in version 0.4.0.

path_line(to=None, relative=False)

Draws a line path from the current point to the given to coordinate. The to coordinates can be relative, or absolute, to the current point by setting the relative flag. The coordinate then becomes the new current point.

Parameters:

New in version 0.4.0.

path_move(to=None, relative=False)

Starts a new sub-path at the given coordinates. Given to parameter can be relative, or absolute, by setting the relative flag.

Parameters:

New in version 0.4.0.

path_start()

Declares the start of a path drawing list which is terminated by a matching path_finish() command. All other path_* commands must be enclosed between a path_start() and a path_finish() command. This is because path drawing commands are subordinate commands and they do not function by themselves.

New in version 0.4.0.

path_vertical_line(y=None, relative=False)

Draws a vertical line path from the current point to the target point. Given y parameter can be relative, or absolute, to the current point by setting the relative flag. The target point then becomes the new current point.

Parameters:
  • y (Real) – Real y-axis point to draw to.
  • relative (bool) – bool treat given point as relative to current point

New in version 0.4.0.

point(x, y)

Draws a point at given x and y

Parameters:

New in version 0.4.0.

polygon(points=None)

Draws a polygon using the current stroke_color, stroke_width, and fill_color, using the specified array of coordinates.

Example polygon on image

with Drawing() as draw:
    points = [(40,10), (20,50), (90,10), (70,40)]
    draw.polygon(points)
    draw.draw(image)
Parameters:points (list) – list of x,y tuples

New in version 0.4.0.

polyline(points=None)

Draws a polyline using the current stroke_color, stroke_width, and fill_color, using the specified array of coordinates.

Identical to polygon, but without closed stroke line.

Parameters:points (list) – list of x,y tuples

New in version 0.4.0.

pop()

Pop destroys the current tip of the drawing context stack, and restores the parent style context. See push() method for an example.

Note

Popping the graphical context stack will not erase, or alter, any previously executed drawing commands.

Returns:success of pop operation.
Return type:bool

New in version 0.4.0.

pop_clip_path()

Terminates a clip path definition.

New in version 0.4.0.

pop_defs()

Terminates a definition list.

New in version 0.4.0.

pop_pattern()

Terminates a pattern definition.

New in version 0.4.0.

push()

Grows the current drawing context stack by one, and inherits the previous style attributes. Use Drawing.pop to return to restore previous style attributes.

This is useful for drawing shapes with diffrent styles without repeatedly setting the similar fill_color & stroke_color properties.

For example:

with Drawing() as ctx:
    ctx.fill_color = Color('GREEN')
    ctx.stroke_color = Color('ORANGE')
    ctx.push()
    ctx.fill_color = Color('RED')
    ctx.text(x1, y1, 'this is RED with ORANGE outline')
    ctx.push()
    ctx.stroke_color = Color('BLACK')
    ctx.text(x2, y2, 'this is RED with BLACK outline')
    ctx.pop()
    ctx.pop()
    ctx.text(x3, y3, 'this is GREEN with ORANGE outline')

Which translate to the following MVG:

push graphic-context
    fill "GREEN"
    stroke "ORANGE"
    push graphic-context
        fill "RED"
        text x1,y1 "this is RED with ORANGE outline"
        push graphic-context
            stroke "BLACK"
            text x2,y2 "this is RED with BLACK outline"
        pop graphic-context
    pop graphic-context
    text x3,y3 "this is GREEN with ORANGE outline"
pop graphic-context

Note

Pushing graphical context does not reset any previously drawn artifacts.

Returns:success of push operation.
Return type:bool

New in version 0.4.0.

push_clip_path(clip_mask_id)

Starts a clip path definition which is comprised of any number of drawing commands and terminated by a Drawing.pop_clip_path command.

Parameters:clip_mask_id (basestring) – string identifier to associate with the clip path.

New in version 0.4.0.

push_defs()

Indicates that commands up to a terminating Drawing.pop_defs command create named elements (e.g. clip-paths, textures, etc.) which may safely be processed earlier for the sake of efficiency.

New in version 0.4.0.

push_pattern(pattern_id, left, top, width, height)

Indicates that subsequent commands up to a Drawing.pop_pattern command comprise the definition of a named pattern. The pattern space is assigned top left corner coordinates, a width and height, and becomes its own drawing space. Anything which can be drawn may be used in a pattern definition. Named patterns may be used as stroke or brush definitions.

Parameters:
  • pattern_id (basestring) – a unique identifier for the pattern.
  • left (numbers.Real) – x ordinate of top left corner.
  • top (numbers.Real) – y ordinate of top left corner.
  • width (numbers.Real) – width of pattern space.
  • height (numbers.Real) – height of pattern space.
Returns:

success of push operation

Return type:

bool

New in version 0.4.0.

rectangle(left=None, top=None, right=None, bottom=None, width=None, height=None, radius=None, xradius=None, yradius=None)

Draws a rectangle using the current stroke_color, stroke_width, and fill_color.

+--------------------------------------------------+
|              ^                         ^         |
|              |                         |         |
|             top                        |         |
|              |                         |         |
|              v                         |         |
| <-- left --> +-------------------+  bottom       |
|              |             ^     |     |         |
|              | <-- width --|---> |     |         |
|              |           height  |     |         |
|              |             |     |     |         |
|              |             v     |     |         |
|              +-------------------+     v         |
| <--------------- right ---------->               |
+--------------------------------------------------+
Parameters:
  • left (numbers.Real) – x-offset of the rectangle to draw
  • top (numbers.Real) – y-offset of the rectangle to draw
  • right (numbers.Real) – second x-offset of the rectangle to draw. this parameter and width parameter are exclusive each other
  • bottom (numbers.Real) – second y-offset of the rectangle to draw. this parameter and height parameter are exclusive each other
  • width (numbers.Real) – the width of the rectangle to draw. this parameter and right parameter are exclusive each other
  • height (numbers.Real) – the height of the rectangle to draw. this parameter and bottom parameter are exclusive each other
  • radius (numbers.Real) – the corner rounding. this is a short-cut for setting both xradius, and yradius
  • xradius (numbers.Real) – the xradius corner in horizontal direction.
  • yradius (numbers.Real) – the yradius corner in vertical direction.

New in version 0.3.6.

Changed in version 0.4.0: Radius keywords added to create rounded rectangle.

rotate(degree)

Applies the specified rotation to the current coordinate space.

Parameters:degree (Real) – degree to rotate

New in version 0.4.0.

scale(x=None, y=None)

Adjusts the scaling factor to apply in the horizontal and vertical directions to the current coordinate space.

Parameters:
  • x (Real) – Horizontal scale factor
  • y (Real) – Vertical scale factor

New in version 0.4.0.

set_fill_pattern_url(url)

Sets the URL to use as a fill pattern for filling objects. Only local URLs (“#identifier”) are supported at this time. These local URLs are normally created by defining a named fill pattern with Drawing.push_pattern & Drawing.pop_pattern.

Parameters:url (basestring) – URL to use to obtain fill pattern.

New in version 0.4.0.

set_stroke_pattern_url(url)

Sets the pattern used for stroking object outlines. Only local URLs (“#identifier”) are supported at this time. These local URLs are normally created by defining a named stroke pattern with Drawing.push_pattern & Drawing.pop_pattern.

Parameters:url (basestring) – URL to use to obtain stroke pattern.

New in version 0.4.0.

skew(x=None, y=None)

Skews the current coordinate system in the horizontal direction if x is given, and vertical direction if y is given.

Parameters:
  • x (Real) – Skew horizontal direction
  • y (Real) – Skew vertical direction

New in version 0.4.0.

stroke_antialias

(bool) Controls whether stroked outlines are antialiased. Stroked outlines are antialiased by default. When antialiasing is disabled stroked pixels are thresholded to determine if the stroke color or underlying canvas color should be used.

It also can be set.

New in version 0.4.0.

stroke_color

(Color) The current color of stroke. It also can be set.

New in version 0.3.3.

stroke_dash_array

(Sequence) - (numbers.Real) An array representing the pattern of dashes & gaps used to stroke paths. It also can be set.

New in version 0.4.0.

stroke_dash_offset

(numbers.Real) The stroke dash offset. It also can be set.

New in version 0.4.0.

stroke_line_cap

(basestring) The stroke line cap. It also can be set.

New in version 0.4.0.

stroke_line_join

(basestring) The stroke line join. It also can be set.

New in version 0.4.0.

stroke_miter_limit

(Integral) The current miter limit. It also can be set.

New in version 0.4.0.

stroke_opacity

(Real) The current stroke opacity. It also can be set.

New in version 0.4.0.

stroke_width

(numbers.Real) The stroke width. It also can be set.

New in version 0.3.3.

text(x, y, body)

Writes a text body into (x, y).

Parameters:
  • x (numbers.Integral) – the left offset where to start writing a text
  • y (numbers.Integral) – the baseline where to start writing text
  • body (basestring) – the body string to write
text_alignment

(basestring) The current text alignment setting. It’s a string value from TEXT_ALIGN_TYPES list. It also can be set.

text_antialias

(bool) The boolean value which represents whether antialiasing is used for text rendering. It also can be set to True or False to switch the setting.

text_decoration

(basestring) The text decoration setting, a string from TEXT_DECORATION_TYPES list. It also can be set.

text_direction

(basestring) The text direction setting. a string from TEXT_DIRECTION_TYPES list. It also can be set.

text_encoding

(basestring) The internally used text encoding setting. Although it also can be set, but it’s not encouraged.

text_interline_spacing

(numbers.Real) The setting of the text line spacing. It also can be set.

text_interword_spacing

(numbers.Real) The setting of the word spacing. It also can be set.

text_kerning

(numbers.Real) The setting of the text kerning. It also can be set.

text_under_color

(Color) The color of a background rectangle to place under text annotations. It also can be set.

translate(x=None, y=None)

Applies a translation to the current coordinate system which moves the coordinate system origin to the specified coordinate.

Parameters:
  • x (Real) – Skew horizontal direction
  • y (Real) – Skew vertical direction

New in version 0.4.0.

vector_graphics

(basestring) The XML text of the Vector Graphics. It also can be set. The drawing-wand XML is experimental, and subject to change.

Setting this property to None will reset all vector graphic properties to the default state.

New in version 0.4.0.

viewbox(left, top, right, bottom)

Viewbox sets the overall canvas size to be recorded with the drawing vector data. Usually this will be specified using the same size as the canvas image. When the vector data is saved to SVG or MVG formats, the viewbox is use to specify the size of the canvas image that a viewer will render the vector data on.

Parameters:
  • left (Integral) – the left most point of the viewbox.
  • top (Integral) – the top most point of the viewbox.
  • right (Integral) – the right most point of the viewbox.
  • bottom (Integral) – the bottom most point of the viewbox.

New in version 0.4.0.

class wand.drawing.FontMetrics(character_width, character_height, ascender, descender, text_width, text_height, maximum_horizontal_advance, x1, y1, x2, y2, x, y)

The tuple subtype which consists of font metrics data.

ascender

Alias for field number 2

character_height

Alias for field number 1

character_width

Alias for field number 0

descender

Alias for field number 3

maximum_horizontal_advance

Alias for field number 6

text_height

Alias for field number 5

text_width

Alias for field number 4

x

Alias for field number 11

x1

Alias for field number 7

x2

Alias for field number 9

y

Alias for field number 12

y1

Alias for field number 8

y2

Alias for field number 10