CINXE.COM
Image data loading
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content="Keras documentation"> <meta name="author" content="Keras Team"> <link rel="shortcut icon" href="https://keras.io/img/favicon.ico"> <link rel="canonical" href="https://keras.io/api/data_loading/image/" /> <!-- Social --> <meta property="og:title" content="Keras documentation: Image data loading"> <meta property="og:image" content="https://keras.io/img/logo-k-keras-wb.png"> <meta name="twitter:title" content="Keras documentation: Image data loading"> <meta name="twitter:image" content="https://keras.io/img/k-keras-social.png"> <meta name="twitter:card" content="summary"> <title>Image data loading</title> <!-- Bootstrap core CSS --> <link href="/css/bootstrap.min.css" rel="stylesheet"> <!-- Custom fonts for this template --> <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700;800&display=swap" rel="stylesheet"> <!-- Custom styles for this template --> <link href="/css/docs.css" rel="stylesheet"> <link href="/css/monokai.css" rel="stylesheet"> <!-- Google Tag Manager --> <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-5DNGF4N'); </script> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-175165319-128', 'auto'); ga('send', 'pageview'); </script> <!-- End Google Tag Manager --> <script async defer src="https://buttons.github.io/buttons.js"></script> </head> <body> <!-- Google Tag Manager (noscript) --> <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5DNGF4N" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> <!-- End Google Tag Manager (noscript) --> <div class='k-page'> <div class="k-nav" id="nav-menu"> <a href='/'><img src='/img/logo-small.png' class='logo-small' /></a> <div class="nav flex-column nav-pills" role="tablist" aria-orientation="vertical"> <a class="nav-link" href="/about/" role="tab" aria-selected="">About Keras</a> <a class="nav-link" href="/getting_started/" role="tab" aria-selected="">Getting started</a> <a class="nav-link" href="/guides/" role="tab" aria-selected="">Developer guides</a> <a class="nav-link" href="/examples/" role="tab" aria-selected="">Code examples</a> <a class="nav-link active" href="/api/" role="tab" aria-selected="">Keras 3 API documentation</a> <a class="nav-sublink" href="/api/models/">Models API</a> <a class="nav-sublink" href="/api/layers/">Layers API</a> <a class="nav-sublink" href="/api/callbacks/">Callbacks API</a> <a class="nav-sublink" href="/api/ops/">Ops API</a> <a class="nav-sublink" href="/api/optimizers/">Optimizers</a> <a class="nav-sublink" href="/api/metrics/">Metrics</a> <a class="nav-sublink" href="/api/losses/">Losses</a> <a class="nav-sublink active" href="/api/data_loading/">Data loading</a> <a class="nav-sublink2 active" href="/api/data_loading/image/">Image data loading</a> <a class="nav-sublink2" href="/api/data_loading/timeseries/">Timeseries data loading</a> <a class="nav-sublink2" href="/api/data_loading/text/">Text data loading</a> <a class="nav-sublink2" href="/api/data_loading/audio/">Audio data loading</a> <a class="nav-sublink" href="/api/datasets/">Built-in small datasets</a> <a class="nav-sublink" href="/api/applications/">Keras Applications</a> <a class="nav-sublink" href="/api/mixed_precision/">Mixed precision</a> <a class="nav-sublink" href="/api/distribution/">Multi-device distribution</a> <a class="nav-sublink" href="/api/random/">RNG API</a> <a class="nav-sublink" href="/api/utils/">Utilities</a> <a class="nav-link" href="/2.18/api/" role="tab" aria-selected="">Keras 2 API documentation</a> <a class="nav-link" href="/keras_tuner/" role="tab" aria-selected="">KerasTuner: Hyperparam Tuning</a> <a class="nav-link" href="/keras_hub/" role="tab" aria-selected="">KerasHub: Pretrained Models</a> </div> </div> <div class='k-main'> <div class='k-main-top'> <script> function displayDropdownMenu() { e = document.getElementById("nav-menu"); if (e.style.display == "block") { e.style.display = "none"; } else { e.style.display = "block"; document.getElementById("dropdown-nav").style.display = "block"; } } function resetMobileUI() { if (window.innerWidth <= 840) { document.getElementById("nav-menu").style.display = "none"; document.getElementById("dropdown-nav").style.display = "block"; } else { document.getElementById("nav-menu").style.display = "block"; document.getElementById("dropdown-nav").style.display = "none"; } var navmenu = document.getElementById("nav-menu"); var menuheight = navmenu.clientHeight; var kmain = document.getElementById("k-main-id"); kmain.style.minHeight = (menuheight + 100) + 'px'; } window.onresize = resetMobileUI; window.addEventListener("load", (event) => { resetMobileUI() }); </script> <div id='dropdown-nav' onclick="displayDropdownMenu();"> <svg viewBox="-20 -20 120 120" width="60" height="60"> <rect width="100" height="20"></rect> <rect y="30" width="100" height="20"></rect> <rect y="60" width="100" height="20"></rect> </svg> </div> <form class="bd-search d-flex align-items-center k-search-form" id="search-form"> <input type="search" class="k-search-input" id="search-input" placeholder="Search Keras documentation..." aria-label="Search Keras documentation..." autocomplete="off"> <button class="k-search-btn"> <svg width="13" height="13" viewBox="0 0 13 13"><title>search</title><path d="m4.8495 7.8226c0.82666 0 1.5262-0.29146 2.0985-0.87438 0.57232-0.58292 0.86378-1.2877 0.87438-2.1144 0.010599-0.82666-0.28086-1.5262-0.87438-2.0985-0.59352-0.57232-1.293-0.86378-2.0985-0.87438-0.8055-0.010599-1.5103 0.28086-2.1144 0.87438-0.60414 0.59352-0.8956 1.293-0.87438 2.0985 0.021197 0.8055 0.31266 1.5103 0.87438 2.1144 0.56172 0.60414 1.2665 0.8956 2.1144 0.87438zm4.4695 0.2115 3.681 3.6819-1.259 1.284-3.6817-3.7 0.0019784-0.69479-0.090043-0.098846c-0.87973 0.76087-1.92 1.1413-3.1207 1.1413-1.3553 0-2.5025-0.46363-3.4417-1.3909s-1.4088-2.0686-1.4088-3.4239c0-1.3553 0.4696-2.4966 1.4088-3.4239 0.9392-0.92727 2.0864-1.3969 3.4417-1.4088 1.3553-0.011889 2.4906 0.45771 3.406 1.4088 0.9154 0.95107 1.379 2.0924 1.3909 3.4239 0 1.2126-0.38043 2.2588-1.1413 3.1385l0.098834 0.090049z"></path></svg> </button> </form> <script> var form = document.getElementById('search-form'); form.onsubmit = function(e) { e.preventDefault(); var query = document.getElementById('search-input').value; window.location.href = '/search.html?query=' + query; return False } </script> </div> <div class='k-main-inner' id='k-main-id'> <div class='k-location-slug'> <span class="k-location-slug-pointer">►</span> <a href='/api/'>Keras 3 API documentation</a> / <a href='/api/data_loading/'>Data loading</a> / Image data loading </div> <div class='k-content'> <h1 id="image-data-loading">Image data loading</h1> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.8.0/keras/src/utils/image_dataset_utils.py#L12">[source]</a></span></p> <h3 id="imagedatasetfromdirectory-function"><code>image_dataset_from_directory</code> function</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">image_dataset_from_directory</span><span class="p">(</span> <span class="n">directory</span><span class="p">,</span> <span class="n">labels</span><span class="o">=</span><span class="s2">"inferred"</span><span class="p">,</span> <span class="n">label_mode</span><span class="o">=</span><span class="s2">"int"</span><span class="p">,</span> <span class="n">class_names</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">color_mode</span><span class="o">=</span><span class="s2">"rgb"</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">image_size</span><span class="o">=</span><span class="p">(</span><span class="mi">256</span><span class="p">,</span> <span class="mi">256</span><span class="p">),</span> <span class="n">shuffle</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">validation_split</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">subset</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">interpolation</span><span class="o">=</span><span class="s2">"bilinear"</span><span class="p">,</span> <span class="n">follow_links</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">crop_to_aspect_ratio</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">pad_to_aspect_ratio</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">data_format</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="p">)</span> </code></pre></div> <p>Generates a <a href="https://www.tensorflow.org/api_docs/python/tf/data/Dataset"><code>tf.data.Dataset</code></a> from image files in a directory.</p> <p>If your directory structure is:</p> <div class="codehilite"><pre><span></span><code>main_directory/ ...class_a/ ......a_image_1.jpg ......a_image_2.jpg ...class_b/ ......b_image_1.jpg ......b_image_2.jpg </code></pre></div> <p>Then calling <code>image_dataset_from_directory(main_directory, labels='inferred')</code> will return a <a href="https://www.tensorflow.org/api_docs/python/tf/data/Dataset"><code>tf.data.Dataset</code></a> that yields batches of images from the subdirectories <code>class_a</code> and <code>class_b</code>, together with labels 0 and 1 (0 corresponding to <code>class_a</code> and 1 corresponding to <code>class_b</code>).</p> <p>Supported image formats: <code>.jpeg</code>, <code>.jpg</code>, <code>.png</code>, <code>.bmp</code>, <code>.gif</code>. Animated gifs are truncated to the first frame.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>directory</strong>: Directory where the data is located. If <code>labels</code> is <code>"inferred"</code>, it should contain subdirectories, each containing images for a class. Otherwise, the directory structure is ignored.</li> <li><strong>labels</strong>: Either <code>"inferred"</code> (labels are generated from the directory structure), <code>None</code> (no labels), or a list/tuple of integer labels of the same size as the number of image files found in the directory. Labels should be sorted according to the alphanumeric order of the image file paths (obtained via <code>os.walk(directory)</code> in Python).</li> <li><strong>label_mode</strong>: String describing the encoding of <code>labels</code>. Options are:<ul> <li><code>"int"</code>: means that the labels are encoded as integers (e.g. for <code>sparse_categorical_crossentropy</code> loss).</li> <li><code>"categorical"</code> means that the labels are encoded as a categorical vector (e.g. for <code>categorical_crossentropy</code> loss).</li> <li><code>"binary"</code> means that the labels (there can be only 2) are encoded as <code>float32</code> scalars with values 0 or 1 (e.g. for <code>binary_crossentropy</code>).</li> <li><code>None</code> (no labels).</li> </ul> </li> <li><strong>class_names</strong>: Only valid if <code>labels</code> is <code>"inferred"</code>. This is the explicit list of class names (must match names of subdirectories). Used to control the order of the classes (otherwise alphanumerical order is used).</li> <li><strong>color_mode</strong>: One of <code>"grayscale"</code>, <code>"rgb"</code>, <code>"rgba"</code>. Whether the images will be converted to have 1, 3, or 4 channels. Defaults to <code>"rgb"</code>.</li> <li><strong>batch_size</strong>: Size of the batches of data. Defaults to 32. If <code>None</code>, the data will not be batched (the dataset will yield individual samples).</li> <li><strong>image_size</strong>: Size to resize images to after they are read from disk, specified as <code>(height, width)</code>. Since the pipeline processes batches of images that must all have the same size, this must be provided. Defaults to <code>(256, 256)</code>.</li> <li><strong>shuffle</strong>: Whether to shuffle the data. Defaults to <code>True</code>. If set to <code>False</code>, sorts the data in alphanumeric order.</li> <li><strong>seed</strong>: Optional random seed for shuffling and transformations.</li> <li><strong>validation_split</strong>: Optional float between 0 and 1, fraction of data to reserve for validation.</li> <li><strong>subset</strong>: Subset of the data to return. One of <code>"training"</code>, <code>"validation"</code>, or <code>"both"</code>. Only used if <code>validation_split</code> is set. When <code>subset="both"</code>, the utility returns a tuple of two datasets (the training and validation datasets respectively).</li> <li><strong>interpolation</strong>: String, the interpolation method used when resizing images. Supports <code>"bilinear"</code>, <code>"nearest"</code>, <code>"bicubic"</code>, <code>"area"</code>, <code>"lanczos3"</code>, <code>"lanczos5"</code>, <code>"gaussian"</code>, <code>"mitchellcubic"</code>. Defaults to <code>"bilinear"</code>.</li> <li><strong>follow_links</strong>: Whether to visit subdirectories pointed to by symlinks. Defaults to <code>False</code>.</li> <li><strong>crop_to_aspect_ratio</strong>: If <code>True</code>, resize the images without aspect ratio distortion. When the original aspect ratio differs from the target aspect ratio, the output image will be cropped so as to return the largest possible window in the image (of size <code>image_size</code>) that matches the target aspect ratio. By default (<code>crop_to_aspect_ratio=False</code>), aspect ratio may not be preserved.</li> <li><strong>pad_to_aspect_ratio</strong>: If <code>True</code>, resize the images without aspect ratio distortion. When the original aspect ratio differs from the target aspect ratio, the output image will be padded so as to return the largest possible window in the image (of size <code>image_size</code>) that matches the target aspect ratio. By default (<code>pad_to_aspect_ratio=False</code>), aspect ratio may not be preserved.</li> <li><strong>data_format</strong>: If None uses keras.config.image_data_format() otherwise either 'channel_last' or 'channel_first'.</li> <li><strong>verbose</strong>: Whether to display number information on classes and number of files found. Defaults to <code>True</code>.</li> </ul> <p><strong>Returns</strong></p> <p>A <a href="https://www.tensorflow.org/api_docs/python/tf/data/Dataset"><code>tf.data.Dataset</code></a> object.</p> <ul> <li>If <code>label_mode</code> is <code>None</code>, it yields <code>float32</code> tensors of shape <code>(batch_size, image_size[0], image_size[1], num_channels)</code>, encoding images (see below for rules regarding <code>num_channels</code>).</li> <li>Otherwise, it yields a tuple <code>(images, labels)</code>, where <code>images</code> has shape <code>(batch_size, image_size[0], image_size[1], num_channels)</code>, and <code>labels</code> follows the format described below.</li> </ul> <p>Rules regarding labels format:</p> <ul> <li>if <code>label_mode</code> is <code>"int"</code>, the labels are an <code>int32</code> tensor of shape <code>(batch_size,)</code>.</li> <li>if <code>label_mode</code> is <code>"binary"</code>, the labels are a <code>float32</code> tensor of 1s and 0s of shape <code>(batch_size, 1)</code>.</li> <li>if <code>label_mode</code> is <code>"categorical"</code>, the labels are a <code>float32</code> tensor of shape <code>(batch_size, num_classes)</code>, representing a one-hot encoding of the class index.</li> </ul> <p>Rules regarding number of channels in the yielded images:</p> <ul> <li>if <code>color_mode</code> is <code>"grayscale"</code>, there's 1 channel in the image tensors.</li> <li>if <code>color_mode</code> is <code>"rgb"</code>, there are 3 channels in the image tensors.</li> <li>if <code>color_mode</code> is <code>"rgba"</code>, there are 4 channels in the image tensors.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.8.0/keras/src/utils/image_utils.py#L187">[source]</a></span></p> <h3 id="loadimg-function"><code>load_img</code> function</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">load_img</span><span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">color_mode</span><span class="o">=</span><span class="s2">"rgb"</span><span class="p">,</span> <span class="n">target_size</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">interpolation</span><span class="o">=</span><span class="s2">"nearest"</span><span class="p">,</span> <span class="n">keep_aspect_ratio</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="p">)</span> </code></pre></div> <p>Loads an image into PIL format.</p> <p><strong>Example</strong></p> <div class="codehilite"><pre><span></span><code><span class="n">image</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">load_img</span><span class="p">(</span><span class="n">image_path</span><span class="p">)</span> <span class="n">input_arr</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">img_to_array</span><span class="p">(</span><span class="n">image</span><span class="p">)</span> <span class="n">input_arr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">input_arr</span><span class="p">])</span> <span class="c1"># Convert single image to a batch.</span> <span class="n">predictions</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">input_arr</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>path</strong>: Path to image file.</li> <li><strong>color_mode</strong>: One of <code>"grayscale"</code>, <code>"rgb"</code>, <code>"rgba"</code>. Default: <code>"rgb"</code>. The desired image format.</li> <li><strong>target_size</strong>: Either <code>None</code> (default to original size) or tuple of ints <code>(img_height, img_width)</code>.</li> <li><strong>interpolation</strong>: Interpolation method used to resample the image if the target size is different from that of the loaded image. Supported methods are <code>"nearest"</code>, <code>"bilinear"</code>, and <code>"bicubic"</code>. If PIL version 1.1.3 or newer is installed, <code>"lanczos"</code> is also supported. If PIL version 3.4.0 or newer is installed, <code>"box"</code> and <code>"hamming"</code> are also supported. By default, <code>"nearest"</code> is used.</li> <li><strong>keep_aspect_ratio</strong>: 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.</li> </ul> <p><strong>Returns</strong></p> <p>A PIL Image instance.</p> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.8.0/keras/src/utils/image_utils.py#L110">[source]</a></span></p> <h3 id="imgtoarray-function"><code>img_to_array</code> function</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">img_to_array</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">data_format</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>Converts a PIL Image instance to a NumPy array.</p> <p><strong>Example</strong></p> <div class="codehilite"><pre><span></span><code><span class="kn">from</span><span class="w"> </span><span class="nn">PIL</span><span class="w"> </span><span class="kn">import</span> <span class="n">Image</span> <span class="n">img_data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span> <span class="n">img</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">array_to_img</span><span class="p">(</span><span class="n">img_data</span><span class="p">)</span> <span class="n">array</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">image</span><span class="o">.</span><span class="n">img_to_array</span><span class="p">(</span><span class="n">img</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>img</strong>: Input PIL Image instance.</li> <li><strong>data_format</strong>: Image data format, can be either <code>"channels_first"</code> or <code>"channels_last"</code>. Defaults to <code>None</code>, in which case the global setting <code>keras.backend.image_data_format()</code> is used (unless you changed it, it defaults to <code>"channels_last"</code>).</li> <li><strong>dtype</strong>: Dtype to use. <code>None</code> means the global setting <code>keras.backend.floatx()</code> is used (unless you changed it, it defaults to <code>"float32"</code>).</li> </ul> <p><strong>Returns</strong></p> <p>A 3D NumPy array.</p> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.8.0/keras/src/utils/image_utils.py#L162">[source]</a></span></p> <h3 id="saveimg-function"><code>save_img</code> function</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">save_img</span><span class="p">(</span> <span class="n">path</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">data_format</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">file_format</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span> <span class="p">)</span> </code></pre></div> <p>Saves an image stored as a NumPy array to a path or file object.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>path</strong>: Path or file object.</li> <li><strong>x</strong>: NumPy array.</li> <li><strong>data_format</strong>: Image data format, either <code>"channels_first"</code> or <code>"channels_last"</code>.</li> <li><strong>file_format</strong>: Optional file format override. If omitted, the format to use is determined from the filename extension. If a file object was used instead of a filename, this parameter should always be used.</li> <li><strong>scale</strong>: Whether to rescale image values to be within <code>[0, 255]</code>.</li> <li><strong>**kwargs</strong>: Additional keyword arguments passed to <code>PIL.Image.save()</code>.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.8.0/keras/src/utils/image_utils.py#L35">[source]</a></span></p> <h3 id="arraytoimg-function"><code>array_to_img</code> function</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">array_to_img</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">data_format</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>Converts a 3D NumPy array to a PIL Image instance.</p> <p><strong>Example</strong></p> <div class="codehilite"><pre><span></span><code><span class="kn">from</span><span class="w"> </span><span class="nn">PIL</span><span class="w"> </span><span class="kn">import</span> <span class="n">Image</span> <span class="n">img</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="p">(</span><span class="mi">100</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span> <span class="n">pil_img</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">array_to_img</span><span class="p">(</span><span class="n">img</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>x</strong>: Input data, in any form that can be converted to a NumPy array.</li> <li><strong>data_format</strong>: Image data format, can be either <code>"channels_first"</code> or <code>"channels_last"</code>. Defaults to <code>None</code>, in which case the global setting <code>keras.backend.image_data_format()</code> is used (unless you changed it, it defaults to <code>"channels_last"</code>).</li> <li><strong>scale</strong>: Whether to rescale the image such that minimum and maximum values are 0 and 255 respectively. Defaults to <code>True</code>.</li> <li><strong>dtype</strong>: Dtype to use. <code>None</code> means the global setting <code>keras.backend.floatx()</code> is used (unless you changed it, it defaults to <code>"float32"</code>). Defaults to <code>None</code>.</li> </ul> <p><strong>Returns</strong></p> <p>A PIL Image instance.</p> <hr /> </div> <div class='k-outline'> <div class='k-outline-depth-1'> <a href='#image-data-loading'>Image data loading</a> </div> <div class='k-outline-depth-3'> <a href='#imagedatasetfromdirectory-function'><code>image_dataset_from_directory</code> function</a> </div> <div class='k-outline-depth-3'> <a href='#loadimg-function'><code>load_img</code> function</a> </div> <div class='k-outline-depth-3'> <a href='#imgtoarray-function'><code>img_to_array</code> function</a> </div> <div class='k-outline-depth-3'> <a href='#saveimg-function'><code>save_img</code> function</a> </div> <div class='k-outline-depth-3'> <a href='#arraytoimg-function'><code>array_to_img</code> function</a> </div> </div> </div> </div> </div> </body> <footer style="float: left; width: 100%; padding: 1em; border-top: solid 1px #bbb;"> <a href="https://policies.google.com/terms">Terms</a> | <a href="https://policies.google.com/privacy">Privacy</a> </footer> </html>