---
title: "affine | pypx"
description: "Matrices describing affine transformation of the plane"
canonical_url: "https://pypx.app/packages/affine"
meta:
  "og:description": "Matrices describing affine transformation of the plane"
  "og:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_affine%2Cversion_3.0.1%2Csummary_Matrices%2Bdescribing%2Baffine%2Btransformation%2Bof%2Bthe%2Bplane%2Clicense_BSD-3-Clause%2Cp_Ii9wYWNrYWdlcy9hZmZpbmUi%2Cs_ymc0FRqkkArybn0x.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "affine 3.0.1"
  "twitter:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_affine%2Cversion_3.0.1%2Csummary_Matrices%2Bdescribing%2Baffine%2Btransformation%2Bof%2Bthe%2Bplane%2Clicense_BSD-3-Clause%2Cp_Ii9wYWNrYWdlcy9hZmZpbmUi%2Cs_ymc0FRqkkArybn0x.png"
---

> Index of every page on this site: [llms.txt](https://pypx.app/llms.txt)

# **affine**

v3.0.1

Matrices describing affine transformation of the plane

11 KB Python 3.9+ py3-none-anyBSD-3-Clause1 deps

[** Diff **](https://pypx.app/packages/affine/diff)

`$ uv add affine`

# Affine

Matrices describing 2D affine transformation of the plane. ![Documentation Status](https://readthedocs.org/projects/affine/badge/?version=latest)

The Affine package is derived from Casey Duncan's Planar package. Please see the copyright statement in [src/affine.py](https://pypx.app/src/affine.py)\_.

## Usage

The 3x3 augmented affine transformation matrix for transformations in two dimensions is illustrated below.

```none
| x' |   | a  b  c | | x |
| y' | = | d  e  f | | y |
| 1  |   | 0  0  1 | | 1 |
```

Matrices can be created by passing the values a, b, c, d, e, f to the affine.Affine constructor or by using its identity(), translation(), scale(), shear(), and rotation() class methods.

```pycon
>>> from affine import Affine
>>> Affine.identity()
Affine(1.0, 0.0, 0.0,
       0.0, 1.0, 0.0)
>>> Affine.translation(1.0, 5.0)
Affine(1.0, 0.0, 1.0,
       0.0, 1.0, 5.0)
>>> Affine.scale(2.0)
Affine(2.0, 0.0, 0.0,
       0.0, 2.0, 0.0)
>>> Affine.shear(45.0, 45.0)  # decimal degrees
Affine(1.0, 0.9999999999999999, 0.0,
       0.9999999999999999, 1.0, 0.0)
>>> Affine.rotation(45.0)     # decimal degrees
Affine(0.7071067811865476, -0.7071067811865475, 0.0,
       0.7071067811865475, 0.7071067811865476, 0.0)
```

These matrices can be applied to (x, y) tuples using the @ matrix multiplier operator to obtain transformed coordinates (x', y').

```pycon
>>> Affine.translation(1.0, 5.0) @ (1.0, 1.0)
(2.0, 6.0)
>>> Affine.rotation(45.0) @ (1.0, 1.0)
(1.1102230246251565e-16, 1.414213562373095)
```

Versions before 3.0.0 only supported the *\`\` operator for matrix multiplication. To use these examples with previous releases, replace \`\`@\`\` with \`\`*.

They may also be multiplied together to combine transformations.

```pycon
>>> Affine.translation(1.0, 5.0) @ Affine.rotation(45.0)
Affine(0.7071067811865476, -0.7071067811865475, 1.0,
       0.7071067811865475, 0.7071067811865476, 5.0)
```

## Usage with GIS data packages

Georeferenced raster datasets use affine transformations to map from image coordinates to world coordinates. The affine.Affine.from\_gdal() class method helps convert [GDAL GeoTransform](https://gdal.org/user/raster_data_model.html#affine-geotransform)\_, sequences of 6 numbers in which the first and fourth are the x and y offsets and the second and sixth are the x and y pixel sizes.

Using a GDAL dataset transformation matrix, the world coordinates (x, y) corresponding to the top left corner of the pixel 100 rows down from the origin can be easily computed.

```pycon
>>> geotransform = (-237481.5, 425.0, 0.0, 237536.4, 0.0, -425.0)
>>> fwd = Affine.from_gdal(*geotransform)
>>> col, row = 0, 100
>>> fwd @ (col, row)
(-237481.5, 195036.4)
```

The reverse transformation is obtained using the \~ inverse operator.

```pycon
>>> rev = ~fwd
>>> rev @ fwd @ (col, row)
(0.0, 99.99999999999999)
```

## **Details **

<dl>

<dt>Version</dt>
<dd>3.0.1</dd>

<dt>License</dt>
<dd>BSD-3-Clause</dd>

<dt>Python</dt>
<dd>&gt;=3.9</dd>

<dt>Maintainer</dt>
<dd>Sean Gillies &lt;sean.gillies@gmail.com&gt;</dd></dl>

## **Links**

- [Source](https://github.com/rasterio/affine)
- [View on PyPI](https://pypi.org/project/affine/)

## **Release Cadence **

**5**

releases in the past year

avg 203 days between releases

## **Maintainers **

- Sean Gillies · [sean.gillies@gmail.com](mailto:sean.gillies@gmail.com)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://pypx.app/#website","@type":"WebSite","description":"The Python Package Index, reimagined. Fast search, dependency insights, download trends, and changelogs — all in one place.","name":"pypx","url":"https://pypx.app/"},{"@id":"https://pypx.app/packages/affine#webpage","@type":"WebPage","description":"Matrices describing affine transformation of the plane","name":"affine","url":"https://pypx.app/packages/affine","isPartOf":{"@id":"https://pypx.app/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://pypx.app/packages/affine"]}]},{"@id":"https://pypx.app/packages/affine#/schema/software-application/1","@type":"SoftwareApplication","applicationCategory":"DeveloperApplication","description":"Matrices describing affine transformation of the plane","license":"BSD-3-Clause","name":"affine","softwareVersion":"3.0.1"}]}
```

## Sitemap

See the full [sitemap](https://pypx.app/sitemap.md) for all pages.
