Repository navigation
fix(utils): convert every Pillow mode to 8-bit BGR or grayscale in pillow_to_cv2 - #2614
Merged
Merged
Conversation
pillow_to_cv2
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2614 +/- ##
=======================================
Coverage 91% 91%
=======================================
Files 78 78
Lines 11590 11601 +11
=======================================
+ Hits 10598 10613 +15
+ Misses 992 988 -4 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Some valid integer modes and downstream grayscale helper paths remain unsupported.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 1
Open (3)
What changed in this PR
Updates pillow_to_cv2 to consistently produce 8-bit OpenCV-compatible arrays from Pillow images.
Changes:
- Normalizes grayscale, deep-integer, alpha, and color modes.
- Adds conversion and annotator coverage.
- Documents the behavior change.
| File | Description |
|---|---|
| tests/utils/test_conversion.py | Adds per-mode and OpenCV parity tests. |
| src/supervision/utils/conversion.py | Implements mode-aware Pillow conversion. |
| docs/changelog.md | Documents the conversion fix. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
raghav-rathi
added a commit
to raghav-rathi/supervision
that referenced
this pull request
Sep 22, 2026
Review follow-up for roboflow#2614. pillow_to_cv2 hands back a single-channel array for L, LA, 1 and deep grayscale modes, but tint_image could not broadcast its BGR overlay into it and grayscale_image fed it to COLOR_BGR2GRAY, so those modes still failed in the two helpers (as L already did). Expand a 2-D scene to BGR first; letterbox_image already documented grayscale input. pillow_to_cv2 now picks the high-byte path from the array dtype (any integer wider than 8 bits, signed or not) instead of a mode list, and returns a writable copy for single-channel modes so annotators can draw into it. The changelog entry links the PR.
…llow_to_cv2 pillow_to_cv2 passed raw mode bytes through: a 1-bit image came back as 0/1, a 16-bit or 32-bit integer image wrapped modulo 256, a grayscale image with alpha (LA, PA) crashed cvtColor with two channels, and CMYK ink values were drawn as RGB. Every annotator, crop_image, resize_image, letterbox_image, plot_image and friends go through this helper when handed a Pillow image. Reduce every mode to what cv2.imread returns for the same picture: 1-bit becomes 0/255, alpha is dropped, deep integer images keep their high byte (as libpng's png_set_strip_16 does), and CMYK, YCbCr, HSV and padded RGB are converted to color through Pillow. RGB, RGBA, L and P convert exactly as before.
Review follow-up for roboflow#2614. pillow_to_cv2 hands back a single-channel array for L, LA, 1 and deep grayscale modes, but tint_image could not broadcast its BGR overlay into it and grayscale_image fed it to COLOR_BGR2GRAY, so those modes still failed in the two helpers (as L already did). Expand a 2-D scene to BGR first; letterbox_image already documented grayscale input. pillow_to_cv2 now picks the high-byte path from the array dtype (any integer wider than 8 bits, signed or not) instead of a mode list, and returns a writable copy for single-channel modes so annotators can draw into it. The changelog entry links the PR.
raghav-rathi
force-pushed
the
fix/pillow-to-cv2-modes
branch
from
September 22, 2026 08:25
9a91d34 to
a91bb2e
Compare
raghav-rathi
force-pushed
the
fix/pillow-to-cv2-modes
branch
from
September 22, 2026 08:30
8fcb4ea to
afbbe57
Compare
Member
|
@raghav-rathi pls avoid force push once the PR review process has started, otherwise we have to restart it and it takes much longer 🦝 |
Borda
approved these changes
Sep 22, 2026
3 tasks done
Borda
added a commit
that referenced
this pull request
Sep 29, 2026
…illow_to_cv2` (#2614) pillow_to_cv2 passed raw mode bytes through: a 1-bit image came back as 0/1, a 16-bit or 32-bit integer image wrapped modulo 256, a grayscale image with alpha (LA, PA) crashed cvtColor with two channels, and CMYK ink values were drawn as RGB. Every annotator, crop_image, resize_image, letterbox_image, plot_image and friends go through this helper when handed a Pillow image. Reduce every mode to what cv2.imread returns for the same picture: 1-bit becomes 0/255, alpha is dropped, deep integer images keep their high byte (as libpng's png_set_strip_16 does), and CMYK, YCbCr, HSV and padded RGB are converted to color through Pillow. RGB, RGBA, L and P convert exactly as before. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: jirka <[email protected]>
2 of 3 tasks
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
pillow_to_cv2passed raw mode bytes through: a 1-bit image came back as0/1, a 16-bit or 32-bit integer image wrapped modulo 256,LA/PAcrashedcvtColorwith a two-channel array, and CMYK ink values were drawn as RGB. Every annotator,crop_image,resize_image,letterbox_image,plot_imageand friends go through this helper when handed aPIL.Image.Every mode is now reduced to what
cv2.imreadreturns for the same picture: 1-bit becomes0/255; alpha is dropped (RGBA -> BGR, LA -> grayscale); 16-bit keeps its high byte, as libpng'spng_set_strip_16does when OpenCV reads a 16-bit PNG as 8-bit (32-bit integers are clipped to the 16-bit range first);Fis clipped like Pillow's ownconvert("L"); CMYK, YCbCr, HSV and RGBX are converted to color through Pillow.RGB,RGBA,LandPare byte-identical to before.Motivation and Context
Closes #2613. Same OpenCV-parity stream as #2591, #2592, #2601, #2602 and #2603, for in-memory Pillow images rather than files.
Changes Made
utils/conversion.py:pillow_to_cv2routes single-channel modes (L,1,LA,La,F) to 8-bit grayscale, deep integer modes (I,I;16*) to their high byte, and everything else throughconvert("RGB")to BGR. Docstring and doctest.tests/utils/test_conversion.py: per-mode expectations (1-bit, LA, 16-bit, 32-bit, float, CMYK, HSV, RGBX, PA), YCbCr within Pillow's round-trip tolerance, a real-OpenCV parity test (pytest.importorskip("cv2")) comparing againstcv2.imreadof the same picture written to disk, andBoxAnnotatoron anLAscene.docs/changelog.md: entry under Unreleased.Out of scope: the annotator wrapper pastes the 8-bit result back into the caller's image, so annotating an
I;16image in place still yields a dark 16-bit image. That is the paste-back, not the conversion.Testing
New tests on
develop: the 1-bit, 16-bit, 32-bit, CMYK and LA cases fail with the values above; theLAannotator case fails withcv2.error. With this change they pass.Without OpenCV (NumPy fallback):
tests/utils tests/annotators tests/key_points735 passed, 1 skipped; full suite 3749 passed, 24 skipped (develop: 3737 / 23).With OpenCV 5.0:
tests/utils/test_conversion.py27 passed, including the on-disk parity test.Doctests pass;
ruff,docformatter,codespellclean;mypyreports the same errors asdevelop.Added/updated tests, and the full suite passes locally
Updated docs (docstrings / mkdocs entry) for new or changed public API
Added a changelog entry in
docs/changelog.mdunderUnreleasedAdditional Notes
While checking parity I found that the OpenCV-free
imreadfallback (_read_pil_source, #2602) andIconAnnotator(#2591) scale 16-bit withrint(v / 256), whereas OpenCV keeps the high byte; the two differ by one whenever the low byte is >= 128 (5000 -> 20 vs 19). This PR follows OpenCV forpillow_to_cv2. Happy to align the fallback in a follow-up if you want them identical.