Skip to content

Add a PadToSquare image transform - #1146

Open
Pierluigi Urru (stwykd) wants to merge 3 commits into
microsoft:mainfrom
stwykd:feature/pad-to-square
Open

Pierluigi Urru (stwykd) wants to merge 3 commits into
microsoft:mainfrom
stwykd:feature/pad-to-square

Conversation

@stwykd

@stwykd Pierluigi Urru (stwykd) commented Nov 2, 2026 •

Copy link
Copy Markdown

Adds PadToSquare to the image processor transforms. It pads an (H, W, C) uint8 image to a square of
side max(H, W), pasting it at row (S - H) / 2 and column (S - W) / 2 (rounded down, the same as
LLaVA's expand2square / PIL paste). The border gets fill: one value per channel or a single value;
the default is black. Square images pass through. Some fixed-resolution vision encoders are trained with
this step before a fixed-size Resize, and no existing transform reproduces it.

{ "operation": { "name": "pad_to_square", "type": "PadToSquare", "attrs": { "fill": [0, 0, 0] } } }

Test: ProcessorTest.TestPadToSquare checks a wide PNG, a tall PNG with an odd width/height
difference (the extra border column goes on the right) and a square JPEG against the decode-only
output (the border is the fill colour, the centre is the original pixels), a single fill value for all
channels, and an out-of-range fill that fails at processor creation. PadToSquare followed by Resize
LINEAR is bit-identical to PIL paste + resize(BILINEAR).

Companion to microsoft/onnxruntime-genai#2842 (a llava model type in ONNX Runtime GenAI), which doesn't need this
PR to land.

cc Sayan Shaw (@sayanshaw24) kunal-vaishnavi

Pads an (H, W, C) image to a centred square of side max(H, W) with a
configurable fill colour, the pad-to-square step some vision encoders are
trained with before a fixed-size resize (LLaVA's expand2square). Square
images pass through unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: af407617-e477-4690-b7f6-38e5f88f661b
@stwykd
Pierluigi Urru (stwykd) requested a review from a team as a code owner November 2, 2026 12:31
Copilot AI balanced review requested due to automatic review settings November 2, 2026 12:31
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The tests omit portrait-image coverage for horizontal centering and odd right-side padding, and undisposed tensor handles leak wrappers.

2 open issues
What changed in this PR

Adds PadToSquare to the image-processing pipeline for vision encoders that require centered square padding before resizing.

Changes:

  • Implements centered uint8 image padding with configurable scalar or per-channel fill.
  • Registers PadToSquare for use in processor configurations.
  • Adds pixel-level tests for wide and already-square images using decode-only reference output.
File Description
test/​pp_api_test/​test_processor.cc Tests padding dimensions, border colors, and preserved image pixels.
test/​data/​processor/​pad_to_square.json Configures padding with an explicit RGB fill.
test/​data/​processor/​decode_only.json Provides unpadded reference pixels for comparison.
shared/​api/​image_transforms.hpp Implements padding and fill validation.
shared/​api/​image_processor.cc Registers the new transform.

Review effort: Balanced

Comment thread test/pp_api_test/test_processor.cc Resolved
Comment thread test/pp_api_test/test_processor.cc Resolved
Holds the result tensors in OrtxObjectPtr so the test no longer leaks
them, and adds a tall image (horizontal offset), a single fill value for
all channels and an out-of-range fill that fails at processor creation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: af407617-e477-4690-b7f6-38e5f88f661b
The tall test image has an odd width/height difference, so it gets a 102
column left border and a 103 column right one. Assert the border widths
of every case, and for the tall image the left border, both image edges
and the right border including its extra column.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: af407617-e477-4690-b7f6-38e5f88f661b
@sushraja-msft

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

@stwykd

Copy link
Copy Markdown
Author

Baiju Meswani (@baijumeswani) kunal-vaishnavi could you review this one too? it's the pad to square preprocessing the genai one needs. the ci-optional failure is pip failing to install build deps (collections.Callable), not this change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants