A PyTorch-compatible transform for aspect-aware image scaling and dynamic mathematical padding.
Project description
Aspect-Pad 📐
A lightweight, PyTorch-compatible computer vision transform that perfectly scales and mathematically pads images without distorting their aspect ratios.
When feeding unconstrained real-world images (like drone photography, medical scans, or OCR inputs) into Convolutional Neural Networks (CNNs), standard resizing often squishes and warps the data. Aspect-Pad intelligently scales the image and dynamically pads the remaining space to create a perfect square (or custom rectangle) tensor, preserving the original spatial features of your dataset.
🚀 Installation
You can install the package directly from PyPI:
pip install aspect-pad
💻 Quick Start
Aspect-Pad works natively with PIL Images and drops directly into standard PyTorch transforms.
import torchvision.transforms as transforms
from PIL import Image
from aspect_pad import AspectPad
# Load a raw, irregularly-shaped image
raw_image = Image.open("messy_drone_photo.jpg")
# Target a 512x512 square tensor, dynamically padding the empty space with black (0)
pipeline = transforms.Compose([
AspectPad(target_size=512, fill=0),
transforms.ToTensor()
])
# Pass the image through the pipeline
tensor_image = pipeline(raw_image)
print(tensor_image.shape) # Output: torch.Size([3, 512, 512])
⚙️ Features
-
Drop-in PyTorch Compatibility: Built to work flawlessly inside torchvision.transforms.Compose.
-
Universal "Anti-Squish" Math: Automatically calculates the delta for wide or tall images and centers the original image perfectly.
-
Custom Target Sizes: Target a single integer for standard square CNNs (e.g., 224 for ResNet) or pass a tuple for specific architectures (e.g., (1920, 1080)).
-
Custom Fill Colors: Support for grayscale padding (e.g., 0 for black), RGB padding (e.g., (255, 255, 255) for white), or any arbitrary background color.
Author: Shin Thant Tun
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 aspect_pad-0.1.1.tar.gz.
File metadata
- Download URL: aspect_pad-0.1.1.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa64156fc29d2b799a865ef5041ecb050e72ea090af902b5334cd659875f1e39
|
|
| MD5 |
8cf0d540e11232029c36ec5fff3e6bac
|
|
| BLAKE2b-256 |
f90e2610bb09f2ef10e9e199f72aca71662c708b7c89d7a7b38cdfa6b013064d
|
File details
Details for the file aspect_pad-0.1.1-py3-none-any.whl.
File metadata
- Download URL: aspect_pad-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70558e87c55a7075e1ff0e2ed96785010fcaf9eb8198664b89832e4546bc4d98
|
|
| MD5 |
b90984a64b13c8de48c910d48f72ab32
|
|
| BLAKE2b-256 |
6a5487392e7b6123e402e35519d55e77f79351a0ffbdc5cc194e91649fbc3974
|