<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v2.15.0/keras/preprocessing/image.py#L449-L622) |

Iterator capable of reading images from a directory on disk.

Inherits From: [`Iterator`](https://www.tensorflow.org/versions/r2.15/api_docs/python/tf/keras/preprocessing/image/Iterator), [`Sequence`](https://www.tensorflow.org/versions/r2.15/api_docs/python/tf/keras/utils/Sequence)

    tf.keras.preprocessing.image.DirectoryIterator(
        directory,
        image_data_generator,
        target_size=(256, 256),
        color_mode='rgb',
        classes=None,
        class_mode='categorical',
        batch_size=32,
        shuffle=True,
        seed=None,
        data_format=None,
        save_to_dir=None,
        save_prefix='',
        save_format='png',
        follow_links=False,
        subset=None,
        interpolation='nearest',
        keep_aspect_ratio=False,
        dtype=None
    )

> [!WARNING]
> **Deprecated:** [`tf.keras.preprocessing.image.DirectoryIterator`](https://www.tensorflow.org/versions/r2.15/api_docs/python/tf/keras/preprocessing/image/DirectoryIterator) is not recommended for new code. Prefer loading images with [`tf.keras.utils.image_dataset_from_directory`](https://www.tensorflow.org/versions/r2.15/api_docs/python/tf/keras/utils/image_dataset_from_directory) and transforming the output [`tf.data.Dataset`](https://www.tensorflow.org/versions/r2.15/api_docs/python/tf/data/Dataset) with preprocessing layers. For more information, see the tutorials for [loading images](https://www.tensorflow.org/tutorials/load_data/images) and [augmenting images](https://www.tensorflow.org/tutorials/images/data_augmentation), as well as the [preprocessing layer guide](https://www.tensorflow.org/guide/keras/preprocessing_layers).

<br />

<br />

<br />

| ## Args ||
|---|---|
| `directory` | Path to the directory to read images from. Each subdirectory in this directory will be considered to contain images from one class, or alternatively you could specify class subdirectories via the `classes` argument. |
| `image_data_generator` | Instance of `ImageDataGenerator` to use for random transformations and normalization. |
| `target_size` | tuple of integers, dimensions to resize input images to. |
| `color_mode` | One of `"rgb"`, `"rgba"`, `"grayscale"`. Color mode to read images. |
| `classes` | Optional list of strings, names of subdirectories containing images from each class (e.g. `["dogs", "cats"]`). It will be computed automatically if not set. |
| `class_mode` | Mode for yielding the targets: <br /> - `"binary"`: binary targets (if there are only two classes), - `"categorical"`: categorical targets, - `"sparse"`: integer targets, - `"input"`: targets are images identical to input images (mainly used to work with autoencoders), - `None`: no targets get yielded (only input images are yielded). |
| `batch_size` | Integer, size of a batch. |
| `shuffle` | Boolean, whether to shuffle the data between epochs. |
| `seed` | Random seed for data shuffling. |
| `data_format` | String, one of `channels_first`, `channels_last`. |
| `save_to_dir` | Optional directory where to save the pictures being yielded, in a viewable format. This is useful for visualizing the random transformations being applied, for debugging purposes. |
| `save_prefix` | String prefix to use for saving sample images (if `save_to_dir` is set). |
| `save_format` | Format to use for saving sample images (if `save_to_dir` is set). |
| `subset` | Subset of data (`"training"` or `"validation"`) if validation_split is set in ImageDataGenerator. |
| `interpolation` | Interpolation method used to resample the image if the target size is different from that of the loaded image. Supported methods are "nearest", "bilinear", and "bicubic". If PIL version 1.1.3 or newer is installed, "lanczos" is also supported. If PIL version 3.4.0 or newer is installed, "box" and "hamming" are also supported. By default, "nearest" is used. |
| `keep_aspect_ratio` | Boolean, whether to resize images to a target size without aspect ratio distortion. The image is cropped in the center with target aspect ratio before resizing. |
| `dtype` | Dtype to use for generated arrays. |

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `filepaths` | List of absolute paths to image files. |
| `labels` | Class labels of every observation. |
| `sample_weight` | <br /> <br /> |

<br />

## Methods

### `next`

[View source](https://github.com/keras-team/keras/tree/v2.15.0/keras/preprocessing/image.py#L158-L168)

    next()

For python 2.x.

<br />

<br />

<br />

| Returns ||
|---|---|
| The next batch. ||

<br />

### `on_epoch_end`

[View source](https://github.com/keras-team/keras/tree/v2.15.0/keras/preprocessing/image.py#L121-L122)

    on_epoch_end()

Method called at the end of every epoch.

### `reset`

[View source](https://github.com/keras-team/keras/tree/v2.15.0/keras/preprocessing/image.py#L124-L125)

    reset()

### `set_processing_attrs`

[View source](https://github.com/keras-team/keras/tree/v2.15.0/keras/preprocessing/image.py#L266-L353)

    set_processing_attrs(
        image_data_generator,
        target_size,
        color_mode,
        data_format,
        save_to_dir,
        save_prefix,
        save_format,
        subset,
        interpolation,
        keep_aspect_ratio
    )

Sets attributes to use later for processing files into a batch.

<br />

<br />

<br />

| Args ||
|---|---|
| `image_data_generator` | Instance of `ImageDataGenerator` to use for random transformations and normalization. |
| `target_size` | tuple of integers, dimensions to resize input images to. |
| `color_mode` | One of `"rgb"`, `"rgba"`, `"grayscale"`. Color mode to read images. |
| `data_format` | String, one of `channels_first`, `channels_last`. |
| `save_to_dir` | Optional directory where to save the pictures being yielded, in a viewable format. This is useful for visualizing the random transformations being applied, for debugging purposes. |
| `save_prefix` | String prefix to use for saving sample images (if `save_to_dir` is set). |
| `save_format` | Format to use for saving sample images (if `save_to_dir` is set). |
| `subset` | Subset of data (`"training"` or `"validation"`) if validation_split is set in ImageDataGenerator. |
| `interpolation` | Interpolation method used to resample the image if the target size is different from that of the loaded image. Supported methods are "nearest", "bilinear", and "bicubic". If PIL version 1.1.3 or newer is installed, "lanczos" is also supported. If PIL version 3.4.0 or newer is installed, "box" and "hamming" are also supported. By default, "nearest" is used. |
| `keep_aspect_ratio` | Boolean, whether to resize images to a target size without aspect ratio distortion. The image is cropped in the center with target aspect ratio before resizing. |

<br />

### `__getitem__`

[View source](https://github.com/keras-team/keras/tree/v2.15.0/keras/preprocessing/image.py#L101-L116)

    __getitem__(
        idx
    )

Gets batch at position `index`.

<br />

<br />

<br />

| Args ||
|---|---|
| `index` | position of the batch in the Sequence. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A batch ||

<br />

### `__iter__`

[View source](https://github.com/keras-team/keras/tree/v2.15.0/keras/preprocessing/image.py#L150-L153)

    __iter__()

Create a generator that iterate over the Sequence.

### `__len__`

[View source](https://github.com/keras-team/keras/tree/v2.15.0/keras/preprocessing/image.py#L118-L119)

    __len__()

Number of batch in the Sequence.

<br />

<br />

<br />

| Returns ||
|---|---|
| The number of batches in the Sequence. ||

<br />

<br />

<br />

<br />

| ## Class Variables ||
|---|---|
| allowed_class_modes | <br /> { 'binary', 'categorical', 'input', 'sparse', None } <br /> |
| white_list_formats | `('png', 'jpg', 'jpeg', 'bmp', 'ppm', 'tif', 'tiff')` |

<br />