def cs_backward(arr, cs='rgb'): """ whatevs to RGB 8-bit """ cs = cs.lower() if cs == 'rgb': return (arr * 255).astype('uint8') elif cs == 'lch': rgb = convert_arr(arr, src=ColorSpace.lch, dst=ColorSpace.rgb) return (rgb * 255).astype('uint8') elif cs == 'lab': rgb = convert_arr(arr, src=ColorSpace.lab, dst=ColorSpace.rgb) return (rgb * 255).astype('uint8') elif cs == 'luv': rgb = convert_arr(arr, src=ColorSpace.luv, dst=ColorSpace.rgb) return (rgb * 255).astype('uint8') elif cs == 'xyz': rgb = convert_arr(arr, src=ColorSpace.xyz, dst=ColorSpace.rgb) return (rgb * 255).astype('uint8')
def test_array_bad_colorspace(): arr = np.random.random((3, 3)) with pytest.raises(ValueError): convert_arr(arr, src="FOO", dst="RGB") with pytest.raises(ValueError): convert_arr(arr, src=999, dst=999)
def test_array_bad_colorspace(): arr = np.random.random((3, 3)) with pytest.raises(ValueError): convert_arr(arr, src='FOO', dst='RGB') with pytest.raises(ValueError): convert_arr(arr, src=999, dst=999)
def test_bad_array_type(): bad = np.random.random((3, 3, 3)).astype("uint8") with pytest.raises(ValueError) as exc: saturate_rgb(bad, 1.1) assert "dtype mismatch" in str(exc.value) with pytest.raises(ValueError) as exc: convert_arr(bad, cs.rgb, cs.lch) assert "dtype mismatch" in str(exc.value)
def test_bad_array_dims(): bad = np.random.random((3, 3)) with pytest.raises(ValueError) as exc: saturate_rgb(bad, 1.1) assert "wrong number of dimensions" in str(exc.value) with pytest.raises(ValueError) as exc: convert_arr(bad, cs.rgb, cs.lch) assert "wrong number of dimensions" in str(exc.value)
def test_bad_array_bands(): bad = np.random.random((2, 3, 3)) with pytest.raises(ValueError) as exc: saturate_rgb(bad, 1.1) assert "3 bands" in str(exc.value) with pytest.raises(ValueError) as exc: convert_arr(bad, cs.rgb, cs.lch) assert "3 bands" in str(exc.value)
def test_bad_array_type(): bad = np.random.random((3, 3, 3)).astype('uint8') with pytest.raises(ValueError) as exc: saturate_rgb(bad, 1.1) assert 'dtype mismatch' in str(exc.value) with pytest.raises(ValueError) as exc: convert_arr(bad, cs.rgb, cs.lch) assert 'dtype mismatch' in str(exc.value)
def test_bad_array_dims(): bad = np.random.random((3, 3)) with pytest.raises(ValueError) as exc: saturate_rgb(bad, 1.1) assert 'wrong number of dimensions' in str(exc.value) with pytest.raises(ValueError) as exc: convert_arr(bad, cs.rgb, cs.lch) assert 'wrong number of dimensions' in str(exc.value)
def test_bad_array_bands(): bad = np.random.random((2, 3, 3)) with pytest.raises(ValueError) as exc: saturate_rgb(bad, 1.1) assert '3 bands' in str(exc.value) with pytest.raises(ValueError) as exc: convert_arr(bad, cs.rgb, cs.lch) assert '3 bands' in str(exc.value)
def cs_forward(arr, cs='rgb'): """ RGB (any dtype) to whatevs """ arrnorm_raw = arr.astype('float64') / np.iinfo(arr.dtype).max arrnorm = arrnorm_raw[0:3] cs = cs.lower() if cs == 'rgb': return arrnorm elif cs == 'lch': return convert_arr(arrnorm, src=ColorSpace.rgb, dst=ColorSpace.lch) elif cs == 'lab': return convert_arr(arrnorm, src=ColorSpace.rgb, dst=ColorSpace.lab) elif cs == 'luv': return convert_arr(arrnorm, src=ColorSpace.rgb, dst=ColorSpace.luv) elif cs == 'xyz': return convert_arr(arrnorm, src=ColorSpace.rgb, dst=ColorSpace.xyz)
def test_arr_lch(pair): rgb, lch = pair rgb = _make_array(*rgb) lch = _make_array(*lch) assert np.allclose(convert_arr(lch, cs.lch, cs.rgb), rgb, atol=0.2)
def test_arr_lch(pair): rgb, lch = pair rgb = _make_array(*rgb) lch = _make_array(*lch) assert np.allclose( convert_arr(lch, cs.lch, cs.rgb), rgb, atol=0.2)