diff --git a/scikits/image/color/colorconv.py b/scikits/image/color/colorconv.py index effb7dc6..8fccf6ba 100644 --- a/scikits/image/color/colorconv.py +++ b/scikits/image/color/colorconv.py @@ -43,8 +43,8 @@ References from __future__ import division -__all__ = ['rgb2hsv', 'hsv2rgb', 'rgb2xyz', 'xyz2rgb', 'rgb2rgbcie', - 'rgbcie2rgb'] +__all__ = ['convert_colorspace', 'rgb2hsv', 'hsv2rgb', 'rgb2xyz', 'xyz2rgb', + 'rgb2rgbcie', 'rgbcie2rgb'] __docformat__ = "restructuredtext en" @@ -52,6 +52,56 @@ import numpy as np from scipy import linalg +def convert_colorspace(arr, fromspace, tospace): + """Convert an image array to a new color space. + + Parameters + ---------- + arr : ndarray + The image to convert. + fromspace : str + The color space to convert from. Valid color space strings are + ['RGB', 'HSV', 'RGB CIE', 'XYZ']. Value may also be specified as lower + case. + tospace : str + The color space to convert to. Valid color space strings are + ['RGB', 'HSV', 'RGB CIE', 'XYZ']. Value may also be specified as lower + case. + + Returns + ------- + newarr : ndarray + The converted image. + + Notes + ----- + Conversion occurs through the "central" RGB color space, i.e. conversion + from XYZ to HSV is implemented as XYZ -> RGB -> HSV instead of directly. + + Examples + -------- + >>> import os + >>> from scikits.image import data_dir + >>> from scikits.image.io import imread + + >>> lena = imread(os.path.join(data_dir, 'lena.png')) + >>> lena_hsv = convert_colorspace(lena, 'RGB', 'HSV') + """ + fromdict = {'RGB':lambda im: im, 'HSV':hsv2rgb, 'RGB CIE':rgbcie2rgb, + 'XYZ':xyz2rgb} + todict = {'RGB':lambda im:im, 'HSV':rgb2hsv, 'RGB CIE':rgb2rgbcie, + 'XYZ':rgb2xyz} + + fromspace = fromspace.upper() + tospace = tospace.upper() + if not fromspace in fromdict.keys(): + raise ValueError, 'fromspace needs to be one of %s'%fromdict.keys() + if not tospace in todict.keys(): + raise ValueError, 'tospace needs to be one of %s'%todict.keys() + + return todict[tospace](fromdict[fromspace](arr)) + + def _prepare_colorarray(arr, dtype="float32"): """Check the shape of the array, and give it the requested type""" if type(arr) != np.ndarray: diff --git a/scikits/image/color/tests/test_colorconv.py b/scikits/image/color/tests/test_colorconv.py index 5fcfe053..9473b456 100644 --- a/scikits/image/color/tests/test_colorconv.py +++ b/scikits/image/color/tests/test_colorconv.py @@ -20,7 +20,8 @@ from scikits.image.io import imread from scikits.image.color import ( rgb2hsv, hsv2rgb, rgb2xyz, xyz2rgb, - rgb2rgbcie, rgbcie2rgb + rgb2rgbcie, rgbcie2rgb, + convert_colorspace ) from scikits.image import data_dir @@ -130,6 +131,25 @@ class TestColorconv(TestCase): assert_almost_equal(rgbcie2rgb(rgb2rgbcie(self.colbars_array)), self.colbars_array) + def test_convert_colorspace(self): + colspaces = ['HSV', 'RGB CIE', 'XYZ'] + colfuncs_from = [hsv2rgb, rgbcie2rgb, xyz2rgb] + colfuncs_to = [rgb2hsv, rgb2rgbcie, rgb2xyz] + + assert_almost_equal(convert_colorspace(self.colbars_array, 'RGB', + 'RGB'), self.colbars_array) + for i, space in enumerate(colspaces): + gt = colfuncs_from[i](self.colbars_array) + assert_almost_equal(convert_colorspace(self.colbars_array, space, + 'RGB'), gt) + gt = colfuncs_to[i](self.colbars_array) + assert_almost_equal(convert_colorspace(self.colbars_array, 'RGB', + space), gt) + + self.assertRaises(ValueError, convert_colorspace, self.colbars_array, + 'nokey', 'XYZ') + self.assertRaises(ValueError, convert_colorspace, self.colbars_array, + 'RGB', 'nokey') if __name__ == "__main__": run_module_suite()