Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aspect_pad-0.1.1.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aspect_pad-0.1.1-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

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

Hashes for aspect_pad-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aa64156fc29d2b799a865ef5041ecb050e72ea090af902b5334cd659875f1e39
MD5 8cf0d540e11232029c36ec5fff3e6bac
BLAKE2b-256 f90e2610bb09f2ef10e9e199f72aca71662c708b7c89d7a7b38cdfa6b013064d

See more details on using hashes here.

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

Hashes for aspect_pad-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 70558e87c55a7075e1ff0e2ed96785010fcaf9eb8198664b89832e4546bc4d98
MD5 b90984a64b13c8de48c910d48f72ab32
BLAKE2b-256 6a5487392e7b6123e402e35519d55e77f79351a0ffbdc5cc194e91649fbc3974

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page