Real space transformations of 2D/3D images in PyTorch
Project description
torch-transform-image
Real space transformations of 2D/3D images in PyTorch
Motivation
This package provides a simple, consistent API for applying affine transformations to 2D/3D images in PyTorch. It enables efficient, GPU-accelerated geometric transformations of images.
Installation
pip install torch-transform-image
Features
- Apply arbitrary affine transformations to 2D and 3D images
- Support for various interpolation methods (nearest, bilinear, bicubic for 2D; nearest, trilinear for 3D)
- Batched operations for efficient processing
- Fully differentiable operations compatible with PyTorch's autograd
Coordinate System
This package uses the same coordinate system as NumPy/PyTorch array indexing:
- For 2D images: coordinates are ordered as
[y, x]for dimensions(height, width) - For 3D images: coordinates are ordered as
[z, y, x]for dimensions(depth, height, width)
Transformation matrices left-multiply homogeneous pixel coordinates ([y, x, 1] for 2D and [z, y, x, 1] for 3D).
Generating Transformation Matrices
The companion package torch-affine-utils provides convenient functions
to generate transformation matrices that work with homogenous pixel coordinates (yxw/zyxw):
from torch_affine_utils.transforms_2d import R, T, S # Rotation, Translation, Scale for 2D
from torch_affine_utils.transforms_3d import Rx, Ry, Rz, T, S # Rotation, Translation, Scale for 3D
Usage
2D Transformations
import torch
from torch_transform_image import affine_transform_image_2d
from torch_affine_utils.transforms_2d import R, T, S # Rotation, Translation, Scale
# Create a test image (28×28)
image = torch.zeros((28, 28), dtype=torch.float32)
image[14, 14] = 1 # Place a dot at the center
# Create a transformation matrix to translate coordinates 4 pixels in y direction
translation = T([4, 0]) # Uses [y, x] coordinate order matching dimensions (h, w)
# Apply the transformation
result = affine_transform_image_2d(
image=image,
matrices=translation,
interpolation='bilinear', # Options: 'nearest', 'bilinear', 'bicubic'
yx_matrices=True, # The generated translations have [y, x] order
)
# Compose multiple transformations
# First translate to origin, then rotate, then translate back
T1 = T([-14, -14]) # Move center to origin
R1 = R(45, yx=True) # Rotate 45 degrees
T2 = T([14, 14]) # Move back
transform = T2 @ R1 @ T1 # Matrix composition (applied right-to-left)
# Apply the composed transformation
rotated = affine_transform_image_2d(
image=image,
matrices=transform,
interpolation='bicubic',
yx_matrices=True,
)
3D Transformations
import torch
from torch_transform_image import affine_transform_image_3d
from torch_affine_utils.transforms_3d import R, T, S # Rotation, Translation, Scale
# Create a test volume (64×64×64)
volume = torch.zeros((64, 64, 64), dtype=torch.float32)
volume[32, 32, 32] = 1 # Place a dot at the center
# Create a transformation matrix (translate coordinates 5 voxels in z direction)
translation = T([5, 0, 0]) # Uses [z, y, x] coordinate order matching dimensions (d, h, w)
# Apply the transformation
result = affine_transform_image_3d(
image=volume,
matrices=translation,
interpolation='trilinear', # Options: 'nearest', 'trilinear'
zyx_matrices=True, # The generated translations have [z, y, x] order
)
How It Works
Under the hood, the package:
- Creates a coordinate grid for the output image
- Applies the transformation matrix to these coordinates
- Samples the input image at the transformed coordinates using the specified interpolation method
This can be unintuitive at times, in the 2D example above the dot will move to [10,14] in the output.
If you want the input image to be transformed according to the matrix, you can use torch.linalg.inv to inverse the direction of the transform.
All operations are performed in PyTorch, making them fully differentiable and GPU-compatible.
License
This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file torch_transform_image-0.1.1.tar.gz.
File metadata
- Download URL: torch_transform_image-0.1.1.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb4fc71b960e29b85c89b860696ab054779e22d1ee3c438177d9c886a4f13c9b
|
|
| MD5 |
0445e7fb0dda2cc93f1bae56643772bb
|
|
| BLAKE2b-256 |
de420cbd2ac77f70978d7be4ff014b1baad93a3688d07d2901b0788b91b1f21a
|
Provenance
The following attestation bundles were made for torch_transform_image-0.1.1.tar.gz:
Publisher:
ci.yml on teamtomo/torch-transform-image
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torch_transform_image-0.1.1.tar.gz -
Subject digest:
cb4fc71b960e29b85c89b860696ab054779e22d1ee3c438177d9c886a4f13c9b - Sigstore transparency entry: 753286872
- Sigstore integration time:
-
Permalink:
teamtomo/torch-transform-image@742b12ab8e41ae9f99543cbceedc99e5aadecb82 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/teamtomo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@742b12ab8e41ae9f99543cbceedc99e5aadecb82 -
Trigger Event:
push
-
Statement type:
File details
Details for the file torch_transform_image-0.1.1-py3-none-any.whl.
File metadata
- Download URL: torch_transform_image-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ca6a81e7ac0fcddbfe2e824e26daaae37959cce457581db50a96b7171555e08
|
|
| MD5 |
fde043d3a76bcfd00e4e98becd922add
|
|
| BLAKE2b-256 |
7b854a65640399cafbbef6c94e4055e4d866f3f41a06fef7459556140908d77f
|
Provenance
The following attestation bundles were made for torch_transform_image-0.1.1-py3-none-any.whl:
Publisher:
ci.yml on teamtomo/torch-transform-image
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torch_transform_image-0.1.1-py3-none-any.whl -
Subject digest:
2ca6a81e7ac0fcddbfe2e824e26daaae37959cce457581db50a96b7171555e08 - Sigstore transparency entry: 753286879
- Sigstore integration time:
-
Permalink:
teamtomo/torch-transform-image@742b12ab8e41ae9f99543cbceedc99e5aadecb82 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/teamtomo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@742b12ab8e41ae9f99543cbceedc99e5aadecb82 -
Trigger Event:
push
-
Statement type: