Skip to content

feat: consistent array shape and data type handling in gamma and color conversion - #204

Open
petercorke wants to merge 4 commits into
mainfrom
feat/dtype-consistent-color
Open

petercorke wants to merge 4 commits into
mainfrom
feat/dtype-consistent-color

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

Makes gamma_encode, gamma_decode and colorspace_convert handle array shape and data type consistently. Found from a note that gamma_decode failed for 1D arrays and colorspace_convert gave wrong results for 1D uint8 arrays; a shape-by-type check showed more.

The rules:

  • The output type is the input type, unless the new dtype argument says otherwise. dtype converts and scales.
  • Integer data (uint8 or uint16) spans 0 to the maximum of its type, and float data spans 0 to 1. Other integer types raise ValueError.
  • HSV, HLS, XYZ, Lab* and Luv* have no 0 to 1 range, so they are always floating point in their natural units (hue in degrees, L* 0 to 100, a* and b* signed). An integer image converted to one of them gives float32, and an integer type as their source or destination raises ValueError.

What was wrong, and what changes for callers:

  • gamma_encode and gamma_decode with the sRGB mapping raised ValueError for a 1D array. They now accept any number of dimensions.
  • uint16 data came back as uint8, and float64 as float32, in some paths. They now keep their type and precision.
  • colorspace_convert on a 3-vector or Nx3 array of uint8 converted 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.
  • An 8-bit image converted to HSV, XYZ or Lab* used to have OpenCV's 8-bit encoding (for example hue 0 to 179). It now gives float32 in natural units, and Image.colorspace no longer casts the result back to the input type. The docstrings carry versionchanged notes.
  • float64 images, and uint16 images to HSV, failed inside OpenCV. They now convert, because the work is done on float32.

Also: Image.colorspace gains dtype and now works for ycrcb, which failed because its plane names had the wrong length. rgb to bgr and back were missing from the conversion table and are added. test_gamma_encode_int_image asserted the old float32 result, so it now expects uint8.

This is a behaviour change for callers, so it is a plain feat with 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 printed hsv and Lab from flowers.colorspace(...) on a uint8 image now say float32 with natural-unit values, so the notebook output needs an errata item.

Testing

New tests/test_color_dtype.py covers 1D, 2D and 3D shapes with uint8, uint16, float32 and float64 for the three functions, the dtype argument, scaling, error cases, round trips and Image.colorspace: 195 cases. They fail on the old code. mypy passes 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

petercorke and others added 4 commits November 22, 2026 07:12
…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>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 59 complexity · 2 duplication

Metric Results
Complexity 59
Duplication 2

View in Codacy

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant