@@ -1317,21 +1317,19 @@ def rgb_to_hsv(arr):
13171317 hsv : (..., 3) ndarray
13181318 Colors converted to hsv values in range [0, 1]
13191319 """
1320- # make sure it is an ndarray
13211320 arr = np .asarray (arr )
13221321
13231322 # check length of the last dimension, should be _some_ sort of rgb
13241323 if arr .shape [- 1 ] != 3 :
13251324 raise ValueError ("Last dimension of input array must be 3; "
13261325 "shape {} was found." .format (arr .shape ))
13271326
1328- in_ndim = arr .ndim
1329- if arr .ndim == 1 :
1330- arr = np .array (arr , ndmin = 2 )
1331-
1332- # make sure we don't have an int image
1333- arr = arr .astype (np .promote_types (arr .dtype , np .float32 ))
1334-
1327+ in_shape = arr .shape
1328+ arr = np .array (
1329+ arr , copy = False ,
1330+ dtype = np .promote_types (arr .dtype , np .float32 ), # Don't work on ints.
1331+ ndmin = 2 , # In case input was 1D.
1332+ )
13351333 out = np .zeros_like (arr )
13361334 arr_max = arr .max (- 1 )
13371335 ipos = arr_max > 0
@@ -1353,10 +1351,7 @@ def rgb_to_hsv(arr):
13531351 out [..., 1 ] = s
13541352 out [..., 2 ] = arr_max
13551353
1356- if in_ndim == 1 :
1357- out .shape = (3 ,)
1358-
1359- return out
1354+ return out .reshape (in_shape )
13601355
13611356
13621357def hsv_to_rgb (hsv ):
@@ -1381,14 +1376,12 @@ def hsv_to_rgb(hsv):
13811376 raise ValueError ("Last dimension of input array must be 3; "
13821377 "shape {shp} was found." .format (shp = hsv .shape ))
13831378
1384- # if we got passed a 1D array, try to treat as
1385- # a single color and reshape as needed
1386- in_ndim = hsv .ndim
1387- if in_ndim == 1 :
1388- hsv = np .array (hsv , ndmin = 2 )
1389-
1390- # make sure we don't have an int image
1391- hsv = hsv .astype (np .promote_types (hsv .dtype , np .float32 ))
1379+ in_shape = hsv .shape
1380+ hsv = np .array (
1381+ hsv , copy = False ,
1382+ dtype = np .promote_types (hsv .dtype , np .float32 ), # Don't work on ints.
1383+ ndmin = 2 , # In case input was 1D.
1384+ )
13921385
13931386 h = hsv [..., 0 ]
13941387 s = hsv [..., 1 ]
@@ -1441,10 +1434,7 @@ def hsv_to_rgb(hsv):
14411434
14421435 rgb = np .stack ([r , g , b ], axis = - 1 )
14431436
1444- if in_ndim == 1 :
1445- rgb .shape = (3 ,)
1446-
1447- return rgb
1437+ return rgb .reshape (in_shape )
14481438
14491439
14501440def _vector_magnitude (arr ):
0 commit comments