Repository navigation
fix(draw): scale 16-bit images down in draw_image - #2603
Merged
Merged
Conversation
draw_image reads image paths with IMREAD_UNCHANGED, which keeps a 16-bit PNG at 16 bits with OpenCV installed. The blend into the 8-bit scene then saturated every nonzero channel to 255. Scale 16-bit images to 8 bits on load, as the IconAnnotator icon loader already does. 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 #2603 +/- ##
=======================================
Coverage 91% 91%
=======================================
Files 78 78
Lines 11484 11486 +2
=======================================
+ Hits 10491 10493 +2
Misses 993 993 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The BGRA test does not verify scaling of semi-transparent 16-bit alpha values.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Fixes color saturation when draw_image loads 16-bit images.
Changes:
- Converts path-loaded
uint16images touint8. - Adds BGR/BGRA tests and changelog documentation.
- Assessment: Code 5/5, Tests 4/5, Docs 5/5.
File summaries
| File | Description |
|---|---|
src/supervision/draw/utils.py |
Scales loaded 16-bit images. |
tests/draw/test_utils.py |
Tests 16-bit BGR/BGRA drawing. |
docs/changelog.md |
Documents the 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]>
Changes: - Add a non-multiple uint16 draw-image regression that asserts rounding 51400 to 201 while retaining semi-transparent BGRA coverage. - Correct the draw-image changelog to describe clipping only for channel values above the 8-bit range. Impact: - Prevents a truncating uint16 conversion from passing the regression test. - Keeps release notes accurate for low-valued 16-bit channels. Verification: - uv run pytest tests/draw/test_utils.py -k 16_bit — 2 passed. - uv run pytest tests/draw — 109 passed. - uv run ruff check and uv run ruff format --check on changed files — passed. - git diff --check — passed. Residual limits: - Full suite has environment failures from the missing metrics extra and one OpenCV harness test; mypy is not installed. - Independent review provenance remains unavailable. --- Co-authored-by: Codex <[email protected]>
Borda
approved these changes
Sep 21, 2026
3 tasks done
Borda
added a commit
that referenced
this pull request
Sep 22, 2026
draw_image reads image paths with IMREAD_UNCHANGED, which keeps a 16-bit PNG at 16 bits with OpenCV installed. The blend into the 8-bit scene then saturated every nonzero channel to 255. Scale 16-bit images to 8 bits on load, as the IconAnnotator icon loader already does. --------- 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: Codex <[email protected]>
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
With OpenCV installed,
sv.draw_imagedraws a 16-bit PNG with every nonzero channel saturated to255:draw_imagereads an image path withcv2.IMREAD_UNCHANGEDto keep the alpha channel. That flag also keeps a 16-bit PNG at 16 bits, but the rest of the function assumes 8 bits:cv2.convertScaleAbssaturates both the alpha and the blended pixels to0..255, so any 16-bit value from256up draws as255. A dark gray logo is drawn white, and so is any color whose three channels are all nonzero.sv.IconAnnotatorreads its icons the same way, and #2591 added the fix there:_load_icon_from_pathscales a 16-bit icon down withnp.clip(np.rint(icon / 256), 0, 255). This PR applies the same scaling indraw_imagewhen it reads an image from a path.Motivation and Context
Image editors export 16-bit PNGs when a document is in 16 bits per channel, and a logo or watermark file passed to
draw_imagecan be one of them. Without OpenCV, the fallback reader already returns a 16-bit color PNG at 8 bits, because Pillow can't hold 16-bit RGB, so the same call drew correctly there and only the OpenCV path was affected.Changes Made
src/supervision/draw/utils.py: aftercv2.imread, scale auint16image down touint8in the same way as_load_icon_from_path. Theimageargument docs say so.tests/draw/test_utils.py:test_draw_image_16_bit_file_draws_its_colors, for BGR and BGRA, returns a 16-bit image from a patchedcv2.imread(asTestIconAnnotator.test_draws_16_bit_color_icondoes) and expects the 8-bit colors in the scene. The patch keeps the test independent of which backend is installed, since the fallback can't write 16-bit color PNGs.docs/changelog.md: entry under Unreleased.Out of scope: arrays passed directly are drawn as before. The parameter is typed
NDArray[np.uint8], and the caller controls what they pass. Grayscale files are still rejected, as the docstring states.Testing
New tests on
develop:It fails the same way without OpenCV, where the fallback's
convertScaleAbssaturates just as OpenCV's does.With this change:
mypyon the changed source and test files reports the same errors ondevelopand on this branch. I also ran the snippet above on real 16-bit BGR and BGRA PNGs with OpenCV installed:[0 0 255]before,[0 0 201]after. Without OpenCV, both give[0 0 200].docs/changelog.mdunderUnreleased(skip for lint/type/format-only or pure doc changes)Additional Notes
The rounding matches the icon loader, so
200 * 257becomes201rather than the fallback's200, which keeps the high byte. I kept the icon loader's rounding rather than add a second convention. The logic is two lines, so I repeated it instead of moving it into a shared helper betweenannotators/core.pyanddraw/utils.py. I'm happy to extract one if you'd prefer.🤖 Generated with Claude Code