fix: support grayscale letterbox images#2297
Merged
Borda merged 3 commits intoJun 7, 2026
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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:
|
Contributor
There was a problem hiding this comment.
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_imageto 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
imageinstead of the paddedimage_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 onimage_with_bordersand compute the start of the bottom/right padding usingresolution_wh(orpadding_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
- 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
letterbox_image's alpha-channel branch so grayscale NumPy images do not crash after resizingTests
uv run pytest tests/utils/test_image.py::test_letterbox_image_for_grayscale_opencv_image -quv run pytest tests/utils/test_image.py -quv run pytest tests/utils -quv run pre-commit run --files src/supervision/utils/image.py tests/utils/test_image.pyuv run pytest -q