Repository navigation
feat: consistent array shape and data type handling in gamma and color conversion - #204
Open
petercorke wants to merge 4 commits into
Open
petercorke wants to merge 4 commits into
petercorke wants to merge 4 commits into
Conversation
…r conversion gamma_encode, gamma_decode and colorspace_convert now return the type of their input unless a new dtype argument says otherwise, with integers spanning 0 to the maximum of uint8 or uint16 and floats spanning 0 to 1. The sRGB gamma accepts any number of dimensions, 16-bit data keeps its precision, integer 3-vectors and Nx3 arrays are scaled instead of being taken as floats, and float64 and uint16 images convert in every colorspace. HSV, HLS, XYZ, L*a*b* and L*u*v* are always floating point in natural units. Image.colorspace gains dtype and works for ycrcb. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The version notes now say only that dtype was added. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The version notes say what changed in 2.5.0 for existing callers. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 59 |
| Duplication | 2 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
This branch has not been deployed
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.
Summary
Makes
gamma_encode,gamma_decodeandcolorspace_converthandle array shape and data type consistently. Found from a note thatgamma_decodefailed for 1D arrays andcolorspace_convertgave wrong results for 1Duint8arrays; a shape-by-type check showed more.The rules:
dtypeargument says otherwise.dtypeconverts and scales.uint8oruint16) spans 0 to the maximum of its type, and float data spans 0 to 1. Other integer types raiseValueError.ValueError.What was wrong, and what changes for callers:
gamma_encodeandgamma_decodewith the sRGB mapping raisedValueErrorfor a 1D array. They now accept any number of dimensions.uint16data came back asuint8, andfloat64as float32, in some paths. They now keep their type and precision.colorspace_converton a 3-vector or Nx3 array ofuint8converted to float32 without scaling, so 255 was treated as 255.0 and the answer was silently wrong. Integer values are now scaled to 0 to 1.Image.colorspaceno longer casts the result back to the input type. The docstrings carryversionchangednotes.float64images, anduint16images to HSV, failed inside OpenCV. They now convert, because the work is done on float32.Also:
Image.colorspacegainsdtypeand now works forycrcb, which failed because its plane names had the wrong length.rgbtobgrand back were missing from the conversion table and are added.test_gamma_encode_int_imageasserted the old float32 result, so it now expectsuint8.This is a behaviour change for callers, so it is a plain
featwith no!marker. Chapter 10 of the book calls these functions: its colour cells (43 to 54 and 82 to 85) all run, and the numbers from its list and float32-image cells are identical. The printedhsvandLabfromflowers.colorspace(...)on auint8image now say float32 with natural-unit values, so the notebook output needs an errata item.Testing
New
tests/test_color_dtype.pycovers 1D, 2D and 3D shapes withuint8,uint16, float32 and float64 for the three functions, thedtypeargument, scaling, error cases, round trips andImage.colorspace: 195 cases. They fail on the old code.mypypasses with zero errors and the full suite gives 1665 passed, 40 skipped. All run on OpenCV 5.0 only. Not run: OpenCV 4, the docs build, and the other Chapter 10 cells.This change was written with AI assistance (Claude Code) and the diff has been reviewed by the maintainer.
🤖 Generated with Claude Code