Skip to content

fix: support grayscale letterbox images#2297

Merged
Borda merged 3 commits into
roboflow:developfrom
Bortlesboat:fix/letterbox-grayscale-image
Jun 7, 2026
Merged

fix: support grayscale letterbox images#2297
Borda merged 3 commits into
roboflow:developfrom
Bortlesboat:fix/letterbox-grayscale-image

Conversation

@Bortlesboat

Copy link
Copy Markdown
Contributor

Summary

  • guard letterbox_image's alpha-channel branch so grayscale NumPy images do not crash after resizing
  • add a regression test covering grayscale padding output

Tests

  • uv run pytest tests/utils/test_image.py::test_letterbox_image_for_grayscale_opencv_image -q
  • uv run pytest tests/utils/test_image.py -q
  • uv run pytest tests/utils -q
  • uv run pre-commit run --files src/supervision/utils/image.py tests/utils/test_image.py
  • uv run pytest -q

@Bortlesboat Bortlesboat requested a review from SkalskiP as a code owner June 7, 2026 15:00
@CLAassistant

CLAassistant commented Jun 7, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@codecov

codecov Bot commented Jun 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79%. Comparing base (35006d7) to head (0d192e5).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2297   +/-   ##
=======================================
  Coverage       79%     79%           
=======================================
  Files           66      66           
  Lines         8640    8635    -5     
=======================================
- Hits          6863    6862    -1     
+ Misses        1777    1773    -4     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Pull request overview

This PR updates letterbox_image to better handle grayscale NumPy images (2D arrays) by guarding alpha-channel handling, and adds a regression test to ensure grayscale padding output is correct.

Changes:

  • Guard the alpha-channel branch in letterbox_image to avoid crashes on grayscale NumPy images.
  • Add a regression test validating letterbox padding behavior for grayscale OpenCV/NumPy images.

Quality Assessment (n/5)

  • Code quality: 2/5
  • Testing: 4/5
  • Documentation: 3/5

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/supervision/utils/image.py Adds a dimension guard around the alpha-channel logic in letterbox_image.
tests/utils/test_image.py Adds a regression test for grayscale NumPy input letterboxing and padding output.
Comments suppressed due to low confidence (1)

src/supervision/utils/image.py:270

  • The alpha-channel padding fix is applied to the original image instead of the padded image_with_borders, so it (a) does not affect the returned value and (b) can unexpectedly mutate the caller’s input array. The indices used (height_new - padding_bottom, width_new - padding_right) are also inconsistent with the bordered image coordinate system and can target the wrong region. Update the block to operate on image_with_borders and compute the start of the bottom/right padding using resolution_wh (or padding_top + height_new, padding_left + width_new).
    if image.ndim == 3 and image.shape[2] == 4:
        image[:padding_top, :, 3] = 0
        image[height_new - padding_bottom :, :, 3] = 0
        image[:, :padding_left, 3] = 0
        image[:, width_new - padding_right :, 3] = 0

Borda and others added 2 commits June 7, 2026 11:03
- Delete letterbox_image alpha block (lines 266-270): block wrote to
  caller's input array, not image_with_borders; used wrong coordinate
  system (resized vs original dims); redundant since cv2.copyMakeBorder
  already sets alpha=0 in padded regions when given a 3-element value
- Add test_letterbox_image_for_rgba_opencv_image: asserts padded alpha=0,
  interior alpha preserved, and input array not mutated after call
- Update letterbox_image docstring: image param lists (H,W,3)/(H,W,4)/
  (H,W)/PIL shapes; add Note on BGRA alpha behavior; add grayscale doctest

---
Co-authored-by: Claude Code <noreply@anthropic.com>
@Borda Borda merged commit b18a30e into roboflow:develop Jun 7, 2026
26 checks passed
@Borda Borda mentioned this pull request Jun 11, 2026
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.

4 participants