Repository navigation
fix(cv2): write JPEG and WebP at OpenCV's default quality without OpenCV - #2592
Merged
Borda merged 9 commits intoSep 18, 2026
Merged
Conversation
Given no parameters, cv2.imwrite and cv2.imencode write JPEG at quality 95 and WebP losslessly. The OpenCV-free fallback used Pillow's defaults, JPEG at quality 75 and lossy WebP, so ImageSink and in-memory dataset exports stored frames differently depending on whether OpenCV was installed. 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 #2592 +/- ##
=======================================
Coverage 91% 91%
=======================================
Files 78 78
Lines 11388 11398 +10
=======================================
+ Hits 10387 10397 +10
Misses 1001 1001 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The fallback still fails for OpenCV’s .jpe JPEG alias despite the PR claiming support for it.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Aligns Pillow-based image encoding with OpenCV’s default JPEG and WebP behavior.
Changes:
- Applies JPEG quality 95 and lossless WebP options.
- Adds fallback and
ImageSinkregression tests. - Documents the behavior change.
File summaries
| File | Description |
|---|---|
src/supervision/_cv2/_image.py |
Configures Pillow encoder defaults. |
tests/cv2/test_image.py |
Compares fallback output with OpenCV. |
tests/utils/test_sinks.py |
Verifies lossless WebP output. |
docs/changelog.md |
Records the fix. |
Review details
- Files reviewed: 4/4 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]>
…gnored params [resolve group] PR roboflow#2592 — items 5 10 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
…le, upper-case-ext cases [resolve group] PR roboflow#2592 — items 6 11 12 13 14 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
…uide divergence [resolve group] PR roboflow#2592 — items 7 8 15 --- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Borda
reviewed
Sep 18, 2026
Borda
reviewed
Sep 18, 2026
Co-authored-by: Jirka Borovec <[email protected]>
Borda
approved these changes
Sep 18, 2026
3 tasks done
Borda
added a commit
that referenced
this pull request
Sep 22, 2026
…nCV (#2592) Given no parameters, cv2.imwrite and cv2.imencode write JPEG at quality 95 and WebP losslessly. The OpenCV-free fallback used Pillow's defaults, JPEG at quality 75 and lossy WebP, so ImageSink and in-memory dataset exports stored frames differently depending on whether OpenCV was installed. --------- 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> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 of 3 tasks
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.ImageSinksaves.jpgframes at JPEG quality 75 and.webpframes as lossy WebP. With OpenCV installed, the same call writes quality 95 and lossless WebP:cv2.imwriteandcv2.imencodeuse OpenCV's encoder defaults when noparamsare given:IMWRITE_JPEG_QUALITY95, and lossless WebP. The fallbacks insupervision/_cv2/_image.pydropparamsand callImage.savewith no options, so Pillow's defaults apply: JPEG quality 75, and lossy WebP at quality 80. Here is the same 320×240 frame through each writer:cv2.imwritedevelop.jpg/.jpeg/.jpe.webp.pngThis PR adds
_opencv_default_save_options(image_format), which returns{"quality": 95}for JPEG and{"lossless": True}for WebP._imwriteand_imencodepass these options toImage.save._imwritelooks the format up from the file extension withImage.registered_extensions(), the same tableImage.saveuses. With quality 95, Pillow's JPEG bytes for that frame are identical to OpenCV's.Motivation and Context
These writers run for anyone without
opencv-python. The fallback is used when OpenCV isn't installed:sv.ImageSink.save_image, for frames saved with a.jpgor.webpname pattern;sv.DetectionDataset.as_yolo,as_pascal_vocandas_coco(throughsave_dataset_images) andsv.ClassificationDataset.as_folder_structure, for images held in memory, which keep their original file names.In each case, the stored images depended on whether OpenCV was installed. JPEGs had stronger compression artifacts, and WebP images, which OpenCV writes losslessly, lost their exact pixels.
Changes Made
src/supervision/_cv2/_image.py: new_opencv_default_save_options, used by_imwrite(format from the file extension) and_imencode(format fromext, as before).tests/cv2/test_image.py:test_fallback_encoders_match_opencv_default_jpeg_qualitycompares the JPEG quantization tables, which the quality setting determines, againstcv2.imencode. It uses tables rather than bytes so that libjpeg builds on different platforms can't break it.test_fallback_encoders_write_webp_losslessly_like_opencvdecodes the WebP and expects both the original pixels andcv2.imencode's round trip. Both tests run for_imwriteand_imencode.tests/utils/test_sinks.py:TestImageSink.test_saves_webp_images_losslesslysaves a frame with a.webppattern and reads it back with Pillow. It runs in the OpenCV-free CI jobs, wheretests/cv2is skipped.docs/changelog.md: entry under Unreleased.Out of scope: explicit
paramsare still ignored by the fallback, as before. Supervision itself never passes them. Formats whose defaults already match stay unchanged, including PNG, BMP and TIFF.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 only mapped the two lossy formats whose defaults differ and that OpenCV's standard wheels write. I left alone AVIF (the
opencv-pythonwheel I tested can't write it) and JPEG 2000 (OpenCV's default output is itself lossy there, while Pillow's is lossless).🤖 Generated with Claude Code