<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/ops/image_ops_impl.py#L3285-L3361) |

Function for `decode_bmp`, `decode_gif`, `decode_jpeg`, and `decode_png`.

#### View aliases


**Main aliases**

[`tf.image.decode_image`](https://www.tensorflow.org/api_docs/python/tf/io/decode_image)
**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.image.decode_image`](https://www.tensorflow.org/api_docs/python/tf/io/decode_image), [`tf.compat.v1.io.decode_image`](https://www.tensorflow.org/api_docs/python/tf/io/decode_image)

<br />

    tf.io.decode_image(
        contents,
        channels=None,
        dtype=https://www.tensorflow.org/api_docs/python/tf/dtypes#uint8,
        name=None,
        expand_animations=True
    )

### Used in the notebooks

| Used in the tutorials |
|---|
| - [Adversarial example using FGSM](https://www.tensorflow.org/tutorials/generative/adversarial_fgsm) - [Neural style transfer](https://www.tensorflow.org/tutorials/generative/style_transfer) - [Image Super Resolution using ESRGAN](https://www.tensorflow.org/hub/tutorials/image_enhancing) - [Fast Style Transfer for Arbitrary Styles](https://www.tensorflow.org/hub/tutorials/tf2_arbitrary_image_stylization) - [Frame interpolation using the FILM model](https://www.tensorflow.org/hub/tutorials/tf_hub_film_example) |

Detects whether an image is a BMP, GIF, JPEG, JPEG XL, WebP or PNG, and
performs the appropriate operation to convert the input bytes `string` into a
`Tensor` of type `dtype`.

> [!NOTE]
> **Note:** `decode_gif` and `decode_webp` return a 4-D array of `[num_frames, height, width, 3]`, as opposed to the other image formats which always return 3-D arrays of the form `[height, width,
> num_channels]`. Make sure to take this into account when constructing your graph if you are intermixing animation with static images. Alternately, set the `expand_animations` argument of this function to `False`, in which case the op will return 3-dimensional tensors and will truncate animations to the first frame.

> [!NOTE]
> **Note:** If the first frame of an animated GIF does not occupy the entire canvas (maximum frame width x maximum frame height), then it fills the unoccupied areas (in the first frame) with zeros (black). For frames after the first frame that does not occupy the entire canvas, it uses the previous frame to fill the unoccupied areas.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `contents` | A `Tensor` of type `string`. 0-D. The encoded image bytes. |
| `channels` | An optional `int`. Defaults to `0`. Number of color channels for the decoded image. |
| `dtype` | The desired DType of the returned `Tensor`. |
| `name` | A name for the operation (optional) |
| `expand_animations` | An optional `bool`. Defaults to `True`. Controls the shape of the returned op's output. If `True`, the returned op will produce a 4-D tensor for all GIFs and WebP images, animated or not, and a 3-D tensor in all other cases. If, `False`, the returned op will produce a 3-D tensor for all file types and will truncate animations to the first frame. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| `Tensor` with type `dtype` and a 3- or 4-dimensional shape, depending on the file type and the value of the `expand_animations` parameter. ||

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | On incorrect number of channels. |

<br />