kornia/kornia-rs

[Feature]: Add pad_to_square utility for image preprocessing

Open

#864 opened on Mar 28, 2026

View on GitHub
 (2 comments) (0 reactions) (0 assignees)Rust (188 forks)auto 404
enhancementhelp wantedtriage

Repository metrics

Stars
 (675 stars)
PR merge metrics
 (PR metrics pending)

Description

🚀 Feature Description

Add a helper function pad_to_square to the kornia-imgproc module that automatically pads an image to a square shape while preserving aspect ratio.

The function computes symmetric padding based on input dimensions and centers the image, simplifying preprocessing workflows for models that require square inputs.

📂 Feature Category

Rust Core Library

💡 Motivation

Many computer vision models, including Vision Transformers (ViT) and Vision-Language Models (VLMs), require square input images.

Currently, users must manually compute padding values before calling spatial_padding, which adds unnecessary boilerplate and increases the chance of errors.

This feature simplifies preprocessing by providing a reusable utility that automatically computes symmetric padding and centers the image, improving usability and reducing repetitive code.

💭 Proposed Solution

Introduce a helper function pad_to_square built on top of the existing spatial_padding API.

The function will:

  • Compute required padding based on input width and height
  • Use Padding2D to define symmetric padding
  • Center the image in the output
  • Delegate padding logic to spatial_padding
  • Validate that the destination image has correct square dimensions

Example API:

pub fn pad_to_square<T, const C: usize, A1: ImageAllocator, A2: ImageAllocator>( src: &Image<T, C, A1>, dst: &mut Image<T, C, A2>, padding_mode: PaddingMode, constant_value: [T; C], ) -> Result<(), ImageError>

This keeps the implementation consistent with existing abstractions while simplifying a common preprocessing task.

📚 Library Reference

This feature builds directly on the existing spatial_padding implementation in kornia-rs.

Similar functionality exists in other computer vision libraries:

  • OpenCV: padding via copyMakeBorder
  • PyTorch / torchvision: transforms like Pad and Resize with square enforcement

This proposal adapts the same concept to kornia-rs while maintaining its API design and abstractions.

🔄 Alternatives Considered

Alternatives Considered

  1. Manual use of spatial_padding Users can directly compute padding values and call spatial_padding. However, this requires repetitive boilerplate code and manual calculation of symmetric padding, which can be error-prone.

  2. Resizing instead of padding Images could be resized to square dimensions instead of padded. However, resizing changes the aspect ratio and can distort image content, which is undesirable for many computer vision tasks.

  3. External preprocessing (outside kornia-rs) Users could handle padding outside the library. However, this reduces usability and breaks consistency with kornia-rs's goal of providing complete image processing utilities.

The proposed pad_to_square helper provides a simple, reusable, and consistent solution within the existing API.

🎯 Use Cases

No response

📝 Additional Context

No response

🤝 Contribution Intent

  • I plan to submit a PR to implement this feature
  • I'm requesting this feature but not planning to implement it

Contributor guide