Repository navigation
fix(annotators): draw grayscale PNG icons in IconAnnotator - #2591
Merged
Borda merged 6 commits intoSep 18, 2026
Merged
Conversation
Icons are read with IMREAD_UNCHANGED to keep alpha, which returns a grayscale PNG without alpha as a 2-D array. The overlay only handles BGR and BGRA, so annotate failed with IndexError. Expand grayscale icons to BGR when loading them. Co-Authored-By: Claude Opus 5 <[email protected]>
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Co-Authored-By: Claude Opus 5 <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2591 +/- ##
=======================================
Coverage 91% 91%
=======================================
Files 78 78
Lines 11382 11388 +6
=======================================
+ Hits 10381 10387 +6
Misses 1001 1001 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The new test method lacks the required return type annotation.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Fixes IconAnnotator so grayscale PNG icons are converted to BGR before resizing and overlaying.
Changes:
- Converts two-dimensional grayscale icons to BGR.
- Adds a pixel-accurate regression test.
- Documents the fix in the changelog.
Assessment: Code quality 4/5; testing 4/5; documentation 5/5.
File summaries
| File | Description |
|---|---|
src/supervision/annotators/core.py |
Normalizes grayscale icons to BGR. |
tests/annotators/test_core.py |
Tests grayscale icon rendering. |
docs/changelog.md |
Records the bug fix. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
…-grayscale-icon # Conflicts: # docs/changelog.md
Challenge log: 3 as-suggested, 1 self-resolved, 0 rejected --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda
reviewed
Sep 18, 2026
Borda
approved these changes
Sep 18, 2026
3 tasks done
3 tasks done
Borda
added a commit
that referenced
this pull request
Sep 22, 2026
Icons are read with IMREAD_UNCHANGED to keep alpha, which returns a grayscale PNG without alpha as a 2-D array. The overlay only handles BGR and BGRA, so annotate failed with IndexError. Expand grayscale icons to BGR when loading them. --------- Co-authored-by: Claude Opus 5 <[email protected]> Co-authored-by: Jirka Borovec <[email protected]> Co-authored-by: Copilot Autofix powered by AI <[email protected]> Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
3 tasks done
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.
Description
sv.IconAnnotatorfails on a grayscale PNG icon without alpha, with or without OpenCV installed:_load_icon_from_pathreads the icon withcv2.imread(path, cv2.IMREAD_UNCHANGED)so an RGBA icon keeps its alpha channel. For a grayscale PNG, that flag also keeps the image as a 2-D(H, W)array._overlay_imageonly handles(H, W, 3)and(H, W, 4)overlays, and it indexesoverlay.shape[2]before anything else.This PR expands a 2-D icon to BGR with
cv2.cvtColor(..., cv2.COLOR_GRAY2BGR)right after loading. That is the arraycv2.imreadwould return for the file withoutIMREAD_UNCHANGED. The icon is then letterboxed and cached as before.Motivation and Context
icon_pathis documented as a path to a PNG image, and grayscale is a standard PNG color type. Single-color icons and logos are often saved that way. PNG optimizers such asoptipngandoxipngalso store an RGB image as grayscale whenever all of its pixels are gray, so an icon that worked can start failing after it has been optimized.Changes Made
src/supervision/annotators/core.py:_load_icon_from_pathconverts a 2-D icon to BGR, and its docstring explains why.tests/annotators/test_core.py:TestIconAnnotator.test_draws_grayscale_icondraws a 16×16 gray PNG icon and checks that exactly its square is painted with its gray value.docs/changelog.md: entry under Unreleased.Out of scope:
sv.draw_imagerejects 2-D images on purpose withValueError: Image must have 3 or 4 channels., so it is unchanged. Color icons, with or without alpha, follow the same code path as before.Testing
New test on
develop, with OpenCV installed and without it (sys.modules["cv2"] = None, as in the OpenCV-free CI jobs):With this change:
mypy src/supervision/annotators/core.pyreports the same errors ondevelopand on this branch. Two of them move down 8 lines.docs/changelog.mdunderUnreleased(skip for lint/type/format-only or pure doc changes)Additional Notes
I made the conversion in the icon loader, not in
_overlay_image, so the loader's output always has the shape the overlay expects. The icon cache also stores the converted array, so the conversion runs once per path and resolution.Related: #2588 fixes the OpenCV-free reader for grayscale PNGs that do have alpha. The two changes are independent.
🤖 Generated with Claude Code