Repository navigation
fix(cv2): read CMYK images unchanged as color without OpenCV - #2602
Merged
Borda merged 3 commits intoSep 21, 2026
Merged
Conversation
With IMREAD_UNCHANGED, OpenCV converts CMYK JPEGs to BGR and CMYK TIFFs to opaque BGRA. The Pillow fallback returned the raw ink channels in BGRA order, so IconAnnotator and draw_image used black ink as alpha. 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 #2602 +/- ##
=======================================
Coverage 91% 91%
=======================================
Files 78 78
Lines 11482 11484 +2
=======================================
+ Hits 10489 10491 +2
Misses 993 993 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The focused implementation matches OpenCV behavior and includes appropriate unit, integration, and documentation updates.
Pull request overview
Aligns Pillow fallback decoding of CMYK JPEG/TIFF images with OpenCV.
Changes:
- Converts CMYK JPEG to BGR and TIFF to opaque BGRA.
- Adds decoder parity and
IconAnnotatorregression tests. - Documents the fix in the changelog.
Assessment: Code quality 5/5 · Testing 5/5 · Documentation 5/5 · Risk 1/5
File summaries
| File | Description |
|---|---|
src/supervision/_cv2/_image.py |
Adds CMYK mode conversion. |
tests/cv2/test_image.py |
Tests OpenCV decoding parity. |
tests/annotators/test_core.py |
Tests CMYK icon rendering. |
docs/changelog.md |
Records the bug 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
approved these changes
Sep 21, 2026
This was referenced Sep 21, 2026
Closed
Borda
added a commit
that referenced
this pull request
Sep 22, 2026
With IMREAD_UNCHANGED, OpenCV converts CMYK JPEGs to BGR and CMYK TIFFs to opaque BGRA. The Pillow fallback returned the raw ink channels in BGRA order, so IconAnnotator and draw_image used black ink as alpha. --------- Co-authored-by: Claude Opus 5 <[email protected]> Co-authored-by: jirka <[email protected]>
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
Without OpenCV installed,
sv.IconAnnotatorandsv.draw_imagedraw a CMYK JPEG or TIFF in the wrong colors, and only where it has black ink. With OpenCV installed, the same calls draw it correctly:Both read the image with
cv2.IMREAD_UNCHANGEDto keep an alpha channel. For CMYK, OpenCV converts to color even with that flag: a CMYK JPEG comes back as BGR, and a CMYK TIFF as BGRA with an opaque alpha channel (libtiff's RGBA reader). The fallback's_opencv_unchanged_modeinsupervision/_cv2/_image.pyhas no case for Pillow'sCMYKmode, so it returned the four ink channels unchanged, and_read_pil_sourcethen reordered them as if they were RGBA. The black channel became alpha, and cyan, magenta and yellow were drawn as red, green and blue. A red logo has no black ink, so it came back fully transparent.This PR maps
CMYKtoRGBfor a JPEG and toRGBA(opaque) for a TIFF. It's the same kind of case as the grayscale-with-alpha, transparent-color and 1-bit modes that #2588 aligned.Motivation and Context
This fallback runs for anyone without
opencv-python. CMYK is the usual color space for images prepared for print, such as logos exported from a print workflow, andIMREAD_UNCHANGEDis howIconAnnotatoranddraw_imageload their images. With OpenCV installed these images already work.Changes Made
src/supervision/_cv2/_image.py:_opencv_unchanged_modereturnsRGBfor a CMYK JPEG andRGBAfor a CMYK TIFF. Docstring updated.tests/cv2/test_image.py:test_fallback_imread_unchanged_converts_cmyk_like_opencvwrites random CMYK pixels, black channel included, as.jpgand.tif, and compares_imread(..., IMREAD_UNCHANGED)withcv2.imread: the shape and dtype must match exactly, and the values must be within 2. Pillow's and libjpeg's CMYK-to-RGB rounding differ by up to 2, which is also true of the existingIMREAD_COLORpath.tests/annotators/test_core.py:TestIconAnnotator.test_draws_cmyk_jpeg_icondraws a CMYK JPEG icon and expects its color. It runs in the OpenCV-free CI jobs, wheretests/cv2is skipped.docs/changelog.md: entry under Unreleased.IMREAD_COLORreads are unchanged. They already went throughconvert("RGB")and match OpenCV.Testing
New tests on
develop, with OpenCV installed:Without OpenCV (
sys.modules["cv2"] = None, as in the OpenCV-free CI jobs):With this change:
mypyon the changed source and test files reports the same errors ondevelopand on this branch.docs/changelog.mdunderUnreleased(skip for lint/type/format-only or pure doc changes)Additional Notes
I compared
_imreadwithcv2.imread(opencv-python 5.0.0) for both flags on JPEG, TIFF (uncompressed and LZW), BMP, GIF, PPM, WebP and JPEG 2000 files in the common Pillow modes. The only other differences I found are rare TIFF variants (CIELab, gray with alpha, RGBA with unassociated alpha, which OpenCV premultiplies, and four samples without an alpha tag), a 16-bit PGM, which Pillow reads as 32-bit integers, and a 16-bit color PNG, which Pillow reads at 8 bits. I left those out of scope.🤖 Generated with Claude Code