Repository navigation
fix(cv2): read transparent and 1-bit PNGs unchanged the way OpenCV does - #2588
Merged
Borda merged 4 commits intoSep 17, 2026
Merged
Conversation
The OpenCV-free backend returned Pillow's pixel layout for IMREAD_UNCHANGED: grayscale with alpha as 2 channels, RGB with a transparent color without alpha, and 1-bit images as booleans. OpenCV returns BGRA and 8-bit arrays for these, so IconAnnotator and draw_image failed on or ignored the transparency of such icons without OpenCV. 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 #2588 +/- ##
=======================================
Coverage 91% 91%
=======================================
Files 78 78
Lines 11372 11382 +10
=======================================
+ Hits 10368 10381 +13
+ Misses 1004 1001 -3 🚀 New features to boost your workflow:
|
2 of 3 tasks
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The focused implementation correctly addresses the reported incompatibilities with comprehensive regression coverage.
Pull request overview
Aligns Pillow-based image loading with OpenCV’s unchanged PNG decoding behavior.
Changes:
- Normalizes grayscale-alpha, transparent RGB/palette, and 1-bit images.
- Adds parity and
IconAnnotatorregression tests. - Documents the fix in the changelog.
- Assessment: code quality 5/5, testing 5/5, documentation 5/5.
File summaries
| File | Description |
|---|---|
src/supervision/_cv2/_image.py |
Adds OpenCV-compatible Pillow mode conversion. |
tests/cv2/test_image.py |
Tests unchanged-read parity with OpenCV. |
tests/annotators/test_core.py |
Tests grayscale-alpha icon rendering. |
docs/changelog.md |
Records the user-visible fix. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Borda
reviewed
Sep 17, 2026
Borda
approved these changes
Sep 17, 2026
2 of 3 tasks
Souptik96
added a commit
to Souptik96/supervision
that referenced
this pull request
Sep 18, 2026
AGENTS.md section 6 requires each changelog entry to link the PR rather than the source issue. The entry was added before this PR had a number, so the link is appended now, in the same trailing style as the roboflow#2582 and roboflow#2588 entries.
Borda
pushed a commit
that referenced
this pull request
Sep 22, 2026
…es (#2588) The OpenCV-free backend returned Pillow's pixel layout for IMREAD_UNCHANGED: grayscale with alpha as 2 channels, RGB with a transparent color without alpha, and 1-bit images as booleans. OpenCV returns BGRA and 8-bit arrays for these, so IconAnnotator and draw_image failed on or ignored the transparency of such icons without OpenCV. --------- Co-authored-by: Claude Opus 5 <[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
Without OpenCV installed,
sv.IconAnnotatorfails on a grayscale PNG icon with an alpha channel:sv.draw_image(scene, "icon.png", ...)fails the same way withValueError: Image must have 3 or 4 channels.Both functions read the file with
cv2.imread(path, cv2.IMREAD_UNCHANGED)so the alpha channel survives. For that flag, the fallback backend insupervision/_cv2/_image.pyreturnednp.asarrayof the Pillow image, which is Pillow's layout rather than OpenCV's. OpenCV never returns a 2-channel or boolean image. I compared the two readers on PNGs in each mode Pillow opens them with (1,L,LA,P,RGB,RGBA,I;16, with and without a transparent color). They differ in three cases:cv2.imread(..., IMREAD_UNCHANGED)developLA)(H, W, 4)uint8 BGRA(H, W, 2)uint8tRNS)(H, W, 4)uint8 BGRA(H, W, 3)uint8, no alpha1)(H, W)uint8,0/255(H, W)boolPalettes already had a special case that expands them to RGB or RGBA, as OpenCV does. This PR moves that case into a small helper,
_opencv_unchanged_mode, which also handles the three modes above. The helper returns the Pillow mode to convert to, and the existing BGR/BGRA reordering does the rest. For all five modes the result is byte-identical tocv2.imread.Motivation and Context
OpenCV is optional since the fallback backend was added, so these reads now run for anyone who installs
supervisionwithoutopencv-python. Grayscale PNGs with alpha are common for single-color icons and logos: PNG optimizers such asoptipngandoxipngstore an RGBA image as gray+alpha whenever every pixel is gray. The same icon works with OpenCV installed and fails without it. An RGB icon with a transparent color doesn't raise, but it is pasted with its transparent pixels drawn black.Changes Made
src/supervision/_cv2/_image.py: new_opencv_unchanged_mode(image)returns"RGBA"for palettes with transparency, grayscale with alpha, and RGB with a transparent color;"RGB"for other palettes;"L"for 1-bit; otherwiseNone._read_pil_sourceconverts to that mode forIMREAD_UNCHANGED. Other modes are read as before.tests/cv2/test_image.py:test_fallback_imread_unchanged_matches_opencv_channels_and_depthchecks shape, dtype and pixels againstcv2.imreadfor gray+alpha, RGB with a transparent color, 1-bit, palette, and palette with a transparent color. The two palette cases already passed and cover the moved lines.tests/annotators/test_core.py:TestIconAnnotator.test_draws_grayscale_icon_with_alphadraws a gray+alpha icon and checks that only its opaque square is drawn. It runs in the OpenCV-free CI jobs, where the parity test above is skipped.docs/changelog.md: entry under Unreleased.Out of scope:
IMREAD_COLORreads already matched OpenCV for all these files. 16-bit color PNGs still read as 8-bit in the fallback, because Pillow can't hold 16-bit RGB. Grayscale PNGs without alpha are still 2-D with both backends.Testing
New tests on
develop, with OpenCV installed:On
develop, without OpenCV (sys.modules["cv2"] = None, as in the OpenCV-free CI jobs):With this change:
mypy src/supervision/_cv2/_image.pyprints the same output ondevelopand on this branch.docs/changelog.mdunderUnreleased(skip for lint/type/format-only or pure doc changes)Additional Notes
I matched OpenCV mode by mode instead of converting every unchanged read to RGBA, because OpenCV itself keeps grayscale PNGs without alpha, RGB PNGs without a transparent color, and 16-bit grayscale PNGs as they are. The existing 16-bit test covers that last case.
🤖 Generated with Claude Code