Affine

2.4.0 · active · verified Thu Apr 09

Affine is a Python library providing matrices that describe two-dimensional affine transformations of the plane. It is commonly used in geospatial contexts, such as with raster data, to transform between image and world coordinates. The current stable version is 2.4.0, with a major rewrite for version 3.0 currently in release candidate stages.

Warnings

Install

Imports

Quickstart

Demonstrates creating identity, translation, scaling, and rotation affine objects, combining them, and applying them to a point.

from affine import Affine

# Create an identity affine transform
identity = Affine.identity()
print(f"Identity: {identity}")

# Create a translation transform (move by 10 units in x, 20 in y)
translation = Affine.translation(10, 20)
print(f"Translation: {translation}")

# Create a scaling transform (scale by 2x)
scaling = Affine.scale(2.0)
print(f"Scaling: {scaling}")

# Combine transforms by multiplication (scaling then translation)
combined = translation * scaling
print(f"Combined (scale then translate): {combined}")

# Apply the combined transform to a point (0, 0)
x, y = 0, 0
x_prime, y_prime = combined * (x, y)
print(f"Original point ({x}, {y}) transformed to ({x_prime}, {y_prime})")

# Example of rotation (rotate by 45 degrees)
rotation = Affine.rotation(45.0)
print(f"Rotation 45 deg: {rotation}")

# Apply rotation to a point (1, 0)
x_rot, y_rot = rotation * (1, 0)
print(f"Point (1,0) rotated 45 deg: ({x_rot:.2f}, {y_rot:.2f})")

view raw JSON →