Skip to content

ImagePath

ImagePath.Path stores mutable two-dimensional vertices for geometry and drawing. Construct a path from coordinate pairs, a flat coordinate sequence, another path, or a contiguous buffer containing native-endian C floats. An integer constructs that many vertices at (0.0, 0.0). Paths own a copy of the input and store coordinates as floating-point values.

from blanket import Image, ImageDraw, ImagePath

path = ImagePath.Path([(1, 1), (8, 1), (4, 8)])
path.transform((2, 0, 3, 0, 2, 3))

image = Image.new("RGB", (24, 24), "white")
ImageDraw.Draw(image).polygon(path, fill="rebeccapurple")

Paths support iteration, len(path), positive and negative indexing, and assignment of coordinate tuples. Contiguous slices return independent paths; nonempty slices with a step other than one, slice assignment, and deletion are unsupported, matching Pillow. tolist() returns copied coordinate pairs; tolist(True) returns a flat list.

getbbox() returns (left, top, right, bottom), or four zeros for an empty path. compact(distance=2) removes vertices whose Manhattan distance from the last retained vertex is less than distance. It modifies the path and returns the number of vertices removed, matching Pillow's implementation.

map(function) replaces each point with function(x, y). transform(matrix, wrap=0) applies the affine matrix (a, b, c, d, e, f):

x_out = a * x + b * y + c
y_out = d * x + e * y + f

With nonzero wrap, the transformed x coordinate uses fmod(x_out, wrap). Both methods modify the path and return None. If a mapping callback raises, earlier mapped points remain updated. Compacting a path from inside its mapping callback raises ValueError.

Pass a path directly to ImageDraw coordinate arguments, including point, line, polygon, and shape bounds. Drawing truncates coordinates toward zero and does not modify the path. Paths contain vertices, with no Bezier commands or SVG path-string syntax.

Reference

Path

Path(xy: int | Sequence[float] | Sequence[tuple[float, float]] | Buffer | Path)

Store coordinate pairs, flat coordinates, native float buffers, or a copy of a path.

An integer creates that many zero-valued vertices. Item assignment is supported; slices produce independent paths. Slice assignment and deletion are unsupported. Paths can be passed directly to ImageDraw methods.

compact

compact(distance: float = 2) -> int

Remove vertices closer than the Manhattan distance; return the number removed.

getbbox

getbbox() -> tuple[float, float, float, float]

Return (left, top, right, bottom), or four zeros for an empty path.

map

map(function: Callable[[float, float], tuple[float, float]]) -> None

Replace each vertex with function(x, y), updating the path in place.

tolist

tolist(flat: bool = False) -> list[tuple[float, float]] | list[float]

Return copied coordinate pairs, or a flat list when flat is true.

transform

transform(matrix: Sequence[float], wrap: float = 0) -> None

Apply (ax + by + c, dx + ey + f), optionally wrapping x with fmod.