# Converting Between Colorspaces.

Image ops work either on individual images or on batches of images, depending on the shape of their input Tensor.

If 3-D, the shape is [height, width, channels], and the Tensor represents one image. If 4-D, the shape is [batch_size, height, width, channels], and the Tensor represents batch_size images.

Currently, channels can usefully be 1, 2, 3, or 4. Single-channel images are grayscale, images with 3 channels are encoded as either RGB or HSV. Images with 2 or 4 channels include an alpha channel, which has to be stripped from the image before passing the image to most image processing functions (and can be re-attached later).

Internally, images are either stored in as one float32 per channel per pixel (implicitly, values are assumed to lie in [0,1)) or one uint8 per channel per pixel (values are assumed to lie in [0,255]).

TensorFlow can convert between images in RGB or HSV. The conversion functions work only on float images, so you need to convert images in other formats using convert_image_dtype.

Example:

# Decode an image and convert it to HSV.
rgb_image = tf.image.decode_png(...,  channels=3)
rgb_image_float = tf.image.convert_image_dtype(rgb_image, tf.float32)
hsv_image = tf.image.rgb_to_hsv(rgb_image)


### tf.image.rgb_to_grayscale(images, name=None)

Converts one or more images from RGB to Grayscale.

Outputs a tensor of the same DType and rank as images. The size of the last dimension of the output is 1, containing the Grayscale value of the pixels.

##### Args:
• images: The RGB tensor to convert. Last dimension must have size 3 and should contain RGB values.
• name: A name for the operation (optional).
##### Returns:

The converted grayscale image(s).

### tf.image.grayscale_to_rgb(images, name=None)

Converts one or more images from Grayscale to RGB.

Outputs a tensor of the same DType and rank as images. The size of the last dimension of the output is 3, containing the RGB value of the pixels.

##### Args:
• images: The Grayscale tensor to convert. Last dimension must be size 1.
• name: A name for the operation (optional).
##### Returns:

The converted grayscale image(s).

### tf.image.hsv_to_rgb(images, name=None)

Convert one or more images from HSV to RGB.

Outputs a tensor of the same shape as the images tensor, containing the RGB value of the pixels. The output is only well defined if the value in images are in [0,1].

See rgb_to_hsv for a description of the HSV encoding.

##### Args:
• images: A Tensor. Must be one of the following types: float32, float64. 1-D or higher rank. HSV data to convert. Last dimension must be size 3.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as images. images converted to RGB.

### tf.image.rgb_to_hsv(images, name=None)

Converts one or more images from RGB to HSV.

Outputs a tensor of the same shape as the images tensor, containing the HSV value of the pixels. The output is only well defined if the value in images are in [0,1].

output[..., 0] contains hue, output[..., 1] contains saturation, and output[..., 2] contains value. All HSV values are in [0,1]. A hue of 0 corresponds to pure red, hue 1/3 is pure green, and 2/3 is pure blue.

##### Args:
• images: A Tensor. Must be one of the following types: float32, float64. 1-D or higher rank. RGB data to convert. Last dimension must be size 3.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as images. images converted to HSV.

### tf.image.convert_image_dtype(image, dtype, saturate=False, name=None)

Convert image to dtype, scaling its values if needed.

Images that are represented using floating point values are expected to have values in the range [0,1). Image data stored in integer data types are expected to have values in the range [0,MAX], where MAX is the largest positive representable number for the data type.

This op converts between data types, scaling the values appropriately before casting.

Note that converting from floating point inputs to integer types may lead to over/underflow problems. Set saturate to True to avoid such problem in problematic conversions. If enabled, saturation will clip the output into the allowed range before performing a potentially dangerous cast (and only before performing such a cast, i.e., when casting from a floating point to an integer type, and when casting from a signed to an unsigned type; saturate has no effect on casts between floats, or on casts that increase the type's range).

##### Args:
• image: An image.
• dtype: A DType to convert image to.
• saturate: If True, clip the input before casting (if necessary).
• name: A name for this operation (optional).
##### Returns:

image, converted to dtype.