AggDraw

The AggDraw library provides a Python interface on top of the AGG library. The library was originally developed by Fredrik Lundh (effbot), but has since been picked up by various developers. It is currently maintained by the PyTroll developer group. The official repository can be found on GitHub:

https://github.com/pytroll/aggdraw

The original documentation by effbot is still available but is out of date with the current version of the library. Original examples will be migrated as time is available (pull requests welcome).

Installation

Aggdraw is available on Linux, macOS, and Windows. It can be installed from PyPI with pip:

pip install aggdraw

Or from conda with the conda-forge channel:

conda install -c conda-forge aggdraw

Free-threading support

Basic free-threading compatibility has been enabled starting with the Python 3.14 wheels of aggdraw 1.4.0. However, only the minimum steps have been taken to let Python 3.14’s free-threaded build know that aggdraw could be used without the GIL. No additional locking or redesign of the library has been done.

Aggdraw itself should have no global state, but each individual object is free to have internal state. It is up to the user to limit interactions for an aggdraw object to a single thread or to protect against concurrent access using locks. Even with this in mind, free-threading support in aggdraw is extremely unstable and experimental. If you have a use case for aggdraw in a free-threading environment please file an issue on GitHub to describe your use case and how it is going.

API

class aggdraw.Pen(color, width=1, opacity=255)

Creates a pen object.

Pen objects define a line color and width to use for drawing lines, paths, and shape outlines with the aggdraw.Draw class.

The pen color can be given as:

  • an RGB tuple, e.g. (255, 255, 0). The alpha channel is taken from the opacity argument.

  • an RGBA tuple, e.g. (255, 255, 0, 128). The fourth element sets the alpha channel directly and overrides opacity entirely.

  • a color string, e.g. "black", "#FFFF00", "#ff0", or "rgb(255, 255, 0)". Any string that PIL.ImageColor.getrgb resolves to an RGB triplet is accepted, including the full set of CSS color names. Strings that resolve to four components, such as "rgba(255, 0, 0, 128)", are not.

  • an integer from 0 to 255 specifying a shade of gray. Values outside that range wrap around, so 300 gives the same gray as 44.

Note that an unrecognized color does not raise an error; it is silently treated as black.

Note that the width of a Pen extends equally on either side of the drawn path. This means that if drawing using a Pen with an odd width (e.g. 1, 3, 5) you may want to align your coordinates to the middle of the target pixels to avoid aliasing:

surface = Draw("RGB", (100, 100), "white")
outline = Pen("black", width=1)
# Results in a 2-pixel-wide gray outline
surface.rectangle((2, 2, 10, 10), pen=outline)
# Results in a 1-pixel-wide black outline
surface.rectangle((2.5, 2.5, 10.5, 10.5), pen=outline)
Parameters:
  • color – The pen color.

  • width (float, optional) – The width of the pen.

  • opacity (int, optional) – The opacity of the pen (from 0 to 255). Defaults to a solid pen.

class aggdraw.Brush(color, opacity=255)

Creates a brush object.

Brush objects define a fill color to use for drawing closed shapes with the aggdraw.Draw class.

The brush color can be given as:

  • an RGB tuple, e.g. (255, 255, 0). The alpha channel is taken from the opacity argument.

  • an RGBA tuple, e.g. (255, 255, 0, 128). The fourth element sets the alpha channel directly and overrides opacity entirely.

  • a color string, e.g. "black", "#FFFF00", "#ff0", or "rgb(255, 255, 0)". Any string that PIL.ImageColor.getrgb resolves to an RGB triplet is accepted, including the full set of CSS color names. Strings that resolve to four components, such as "rgba(255, 0, 0, 128)", are not.

  • an integer from 0 to 255 specifying a shade of gray. Values outside that range wrap around, so 300 gives the same gray as 44.

Note that an unrecognized color does not raise an error; it is silently treated as black.

Parameters:
  • color – The brush color.

  • opacity (int, optional) – The opacity of the brush (from 0 to 255). Defaults to a solid brush.

class aggdraw.Font(color, file, size=12, opacity=255)

Creates a font object.

This creates a font object for use with text() and textsize() from a TrueType font file.

The font color can be given as:

  • an RGB tuple, e.g. (255, 255, 0). The alpha channel is taken from the opacity argument.

  • an RGBA tuple, e.g. (255, 255, 0, 128). The fourth element sets the alpha channel directly and overrides opacity entirely.

  • a color string, e.g. "black", "#FFFF00", "#ff0", or "rgb(255, 255, 0)". Any string that PIL.ImageColor.getrgb resolves to an RGB triplet is accepted, including the full set of CSS color names. Strings that resolve to four components, such as "rgba(255, 0, 0, 128)", are not.

  • an integer from 0 to 255 specifying a shade of gray. Values outside that range wrap around, so 300 gives the same gray as 44.

Note that an unrecognized color does not raise an error; it is silently treated as black.

Parameters:
  • color – The font color.

  • file – Path to a valid TrueType font file.

  • size (optional) – The font size (in pixels). Defaults to 12.

  • opacity (int, optional) – The opacity of the font (from 0 to 255). Defaults to solid.

class aggdraw.Path(path=None)

Path factory.

Path objects allow you to define custom paths and shapes that can be drawn with path() or symbol().

Paths are created sequentially, with each new line and curve segment connected to the end of the previous segment (unless the current path position is changed manually using moveto() or rmoveto()).

Parameters:

path (list) – A Python sequence in the format (x, y, x, y, …) defining an initial set of connected line segments with which to initialize the path.

close()

Closes the current path.

This method adds a line segment connecting the end of the current segment to the start of the path. If the path position has been moved at any point by moveto() or rmoveto(), the current segment will instead be connected to the start of the first segment since the path position was last moved.

A subpath needs at least three distinct points to be closed. Calling this method on a subpath with fewer will remove it from the drawing entirely, including the segments already added to it by lineto(). This is a bug in the underlying AGG C++ library.

Closing a subpath of collinear points (no enclosed area) may produce unexpected results as AGG tries to draw a closing segment over the existing drawn segments. This can create incomplete line segments (i.e. “slivers”) or other artifacts.

coords()

Returns the coordinates for the path.

Curves are flattened before being returned.

Returns:

A sequence in (x, y, x, y, …) format.

Return type:

list

curveto(x1, y1, x2, y2, x, y)

Adds a cubic bezier curve segment to the path.

The added curve will be between the path’s current position and the specified endpoint.

Parameters:
  • x1 (float) – The x coordinate of the curve’s first control point.

  • y1 (float) – The y coordinate of the curve’s first control point.

  • x2 (float) – The x coordinate of the curve’s second control point.

  • y2 (float) – The y coordinate of the curve’s second control point.

  • x (float) – The x coordinate of the curve’s endpoint.

  • y (float) – The y coordinate of the curve’s endpoint.

lineto(x, y)

Adds a line segment to the path.

The added line will be between the path’s current position and the specified endpoint.

Parameters:
  • x (float) – The x coordinate of the line’s endpoint.

  • y (float) – The y coordinate of the line’s endpoint.

moveto(x, y)

Moves the path position to the given location.

Moving the path position will change the start point of the next segment added to the path without adding a new segment, creating a gap between the previous segment and the next.

Parameters:
  • x (float) – The x coordinate of the new path position.

  • y (float) – The y coordinate of the new path position.

rcurveto(x1, y1, x2, y2, x, y)

Adds a cubic bezier curve segment to the path using relative coordinates.

Same as curveto(), but the coordinates are relative to the current position.

rlineto(x, y)

Adds a line segment to the path using relative coordinates.

Same as lineto(), but the coordinates are relative to the current position.

rmoveto(x, y)

Moves the path position relative to the current position.

For example, if the current path position was (10, 10), calling p.rmoveto(-5, 5) would change the current position to (5, 15).

Parameters:
  • x (float) – The change in x coordinates relative to the current position.

  • y (float) – The change in y coordinates relative to the current position.

class aggdraw.Symbol(path, scale=1.0)

Symbol factory.

This creates a symbol object from an SVG-style path descriptor for use with symbol().

The following operators are supported:
  • M (move)

  • L (line)

  • H (horizontal line)

  • V (vertical line)

  • C (cubic bezier)

  • S (smooth cubic bezier)

  • Q (quadratic bezier)

  • T (smooth quadratic bezier)

  • Z (close path)

Use lower-case operators for relative coordinates, upper-case for absolute coordinates.

Parameters:
  • path (str) – An SVG-style path descriptor.

  • scale (float, optional) – A multiplier applied to every coordinate in the path descriptor as it is parsed. Defaults to 1.0.

class aggdraw.Draw(image_or_mode, size=None, color='white')

Creates a drawing interface object.

The constructor can either take a PIL Image object, or mode and size specifiers.

Examples:

d = aggdraw.Draw(im)
d = aggdraw.Draw("RGB", (800, 600), "white")
Parameters:
  • image_or_mode – A PIL image or a mode string. The following modes are supported: "L", "RGB", "RGBA", "BGR", "BGRA".

  • size (tuple, optional) – The size of the image (width, height).

  • color (optional) – An optional background color. If omitted, defaults to white with full alpha.

property size

The size in pixels of the draw surface (width, height).

Type:

tuple

property mode

The mode of the draw surface (e.g. “RGBA”).

Type:

str

arc(xy, start, end, pen=None)

Draws an arc.

Parameters:
  • xy – A 4-element Python sequence (x, y, x, y) with the upper-left corner given first.

  • start (float) – The start angle of the arc.

  • end (float) – The end angle of the arc.

  • pen (aggdraw.Pen, optional) – A pen to use for drawing the arc.

chord(xy, start, end, pen=None, brush=None)

Draws a chord.

If a brush is given, it is used to fill the chord. If a pen is given, it is used to draw an outline around the chord. Either one (or both) can be left out.

Parameters:
  • xy – A 4-element Python sequence (x, y, x, y) with the upper-left corner given first.

  • start (float) – The start angle of the chord.

  • end (float) – The end angle of the chord.

  • pen (aggdraw.Pen, optional) – A pen to use for drawing an outline around the chord.

  • brush (aggdraw.Brush, optional) – A brush to use for filling the chord.

ellipse(xy, pen=None, brush=None)

Draws an ellipse.

If a brush is given, it is used to fill the ellipse. If a pen is given, it is used to draw an outline around the ellipse. Either one (or both) can be left out.

To draw a circle, make sure the coordinates form a square.

Parameters:
  • xy – A bounding rectangle as a 4-element Python sequence (x, y, x, y), with the upper-left corner given first.

  • pen (aggdraw.Pen, optional) – A pen to use for drawing an outline around the ellipse.

  • brush (aggdraw.Brush, optional) – A brush to use for filling the ellipse.

flush()

Updates the associated image.

If the drawing area is attached to a PIL Image object, this method must be called to make sure that the image updated.

frombytes(data)

Copies data from a bytes object to the drawing area.

Parameters:

data (bytes) – Packed image data compatible with PIL’s tobytes method.

line(xy, pen=None)

Draws a line.

If the sequence contains multiple x/y pairs, multiple connected lines will be drawn.

Parameters:
  • xy – A Python sequence in the format (x, y, x, y, …) or an aggdraw.Path.

  • pen (aggdraw.Pen, optional) – A pen to use for drawing the line.

path(path, pen=None, brush=None)

Draws a path to the surface.

If a brush is given, it is used to fill the path. If a pen is given, it is used to draw an outline around the path. Either one (or both) can be left out.

This method draws the path without translation (using the coordinates specified when defining the aggdraw.Path). To draw a path at a specific location on the surface, see symbol().

Parameters:
  • path (aggdraw.Path) – The Path object to draw.

  • pen (aggdraw.Pen, optional) – A pen to use for drawing an outline around the path.

  • brush (aggdraw.Brush, optional) – A brush to use for filling the path.

pieslice(xy, start, end, pen=None, brush=None)

Draws a pie slice.

If a brush is given, it is used to fill the pie slice. If a pen is given, it is used to draw an outline around the pie slice. Either one (or both) can be left out.

Parameters:
  • xy – A 4-element Python sequence (x, y, x, y) with the upper-left corner given first.

  • start (float) – The start angle of the pie slice.

  • end (float) – The end angle of the pie slice.

  • pen (aggdraw.Pen, optional) – A pen to use for drawing an outline around the pie slice.

  • brush (aggdraw.Brush, optional) – A brush to use for filling the pie slice.

polygon(xy, pen=None, brush=None)

Draws a polygon.

If a brush is given, it is used to fill the polygon. If a pen is given, it is used to draw an outline around the polygon. Either one (or both) can be left out.

A sequence of coordinates is always closed, so the outline includes a segment joining the last point back to the first. A aggdraw.Path is drawn exactly as it was defined instead: it is closed only where close() was called on it, and is otherwise left open. This only affects the outline drawn by a pen, as filling with a brush closes the shape either way.

Parameters:
  • xy – A Python sequence (x, y, x, y, …) or an aggdraw.Path.

  • pen (aggdraw.Pen, optional) – A pen to use for drawing an outline around the polygon.

  • brush (aggdraw.Brush, optional) – A brush to use for filling the polygon.

rectangle(xy, pen=None, brush=None)

Draws a rectangle.

If a brush is given, it is used to fill the rectangle. If a pen is given, it is used to draw an outline around the rectangle. Either one (or both) can be left out.

Parameters:
  • xy – A 4-element Python sequence (x, y, x, y), with the upper left corner given first.

  • pen (aggdraw.Pen, optional) – A pen to use for drawing an outline around the rectangle.

  • brush (aggdraw.Brush, optional) – A brush to use for filling the rectangle.

rounded_rectangle(xy, radius, pen=None, brush=None)

Draws a rounded rectangle.

If a brush is given, it is used to fill the rectangle. If a pen is given, it is used to draw an outline around the rectangle. Either one (or both) can be left out.

Parameters:
  • xy – A 4-element Python sequence (x, y, x, y), with the upper left corner given first.

  • radius (float) – The corner radius.

  • pen (aggdraw.Pen, optional) – A pen to use for drawing an outline around the rectangle.

  • brush (aggdraw.Brush, optional) – A brush to use for filling the rectangle.

setantialias(flag)

Controls anti-aliasing.

Parameters:

flag (bool) – True to enable anti-aliasing, False to disable it.

settransform(transform=None)

Replaces the current drawing transform.

The transform must either be a (dx, dy) translation tuple, or a PIL-style (a, b, c, d, e, f) affine transform tuple. If the transform is omitted, it is reset.

Example:

draw.settransform((dx, dy))
Parameters:

transform (tuple, optional) – The new transform, or None to reset.

symbol(xy, symbol, pen=None, brush=None)

Draws a symbol at the given positions.

If a brush is given, it is used to fill the symbol. If a pen is given, it is used to draw an outline around the symbol. Either one (or both) can be left out.

This method can be used to draw both aggdraw.Symbol or aggdraw.Path objects.

Parameters:
  • xy – A Python sequence in the format (x, y, x, y, …)

  • symbol (aggdraw.Symbol, aggdraw.Path) – The Symbol (or Path) object to draw.

  • pen (aggdraw.Pen, optional) – A pen to use for drawing an outline around the symbol.

  • brush (aggdraw.Brush, optional) – A brush to use for filling the symbol.

text(xy, text, font)

Draws a text string at a given position using a given font.

Example:

font = aggdraw.Font(black, times)
draw.text((100, 100), "hello, world", font)
Parameters:
  • xy – A 2-element Python sequence (x, y).

  • text (str) – A string of text to render.

  • font (aggdraw.Font) – The font object to render with.

textsize(text, font)

Determines the size of a text string.

Parameters:
  • text (str) – A string of text to measure.

  • font (aggdraw.Font) – The font object to render with.

Returns:

A (width, height) tuple.

Return type:

tuple

tobytes()

Copies data from the drawing area to a bytes object.

Returns:

Packed image data compatible with PIL’s frombytes method.

Return type:

bytes

Indices and tables