How to Load, Convert, and Save Images With the Keras API

The Keras deep learning library provides a sophisticated API for loading, preparing, and augmenting image data.

Also included in the API are some undocumented functions that allow you to quickly and easily load, convert, and save image files. These functions can be convenient when getting started on a computer vision deep learning project, allowing you to use the same Keras API initially to inspect and handle image data.

In this tutorial, you will discover how to use the basic image handling functions provided by the Keras API.

After completing this tutorial, you will know:

  • How to load and display an image using the Keras API.
  • How to convert a loaded image to a NumPy array and back to PIL format using the Keras API.
  • How to convert a loaded image to grayscale and save it to a new file using the Keras API.

Kick-start your project with my new book Deep Learning for Computer Vision, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

Tutorial Overview

This tutorial is divided into five parts; they are:

  1. Test Image
  2. Keras Image Processing API
  3. How to Load an Image With Keras
  4. Convert Image an With Keras
  5. Save Image an With Keras

Test Image

The first step is to select a test image to use in this tutorial.

We will use a photograph of Bondi Beach, Sydney, taken by Isabell Schulz, released under a permissive creative commons license.

Bondi Beach, Sydney

Bondi Beach, Sydney

Download the image and place it into your current working directory with the filename “bondi_beach.jpg“.

Keras Image Processing API

The Keras deep learning library provides utilities for working with image data.

The main API is the ImageDataGenerator class that combines data loading, preparation, and augmentation.

We will not cover the ImageDataGenerator class in this tutorial. Instead, we will take a closer look at a few less-documented or undocumented functions that may be useful when working with image data and modeling with the Keras API.

Specifically, Keras provides functions for loading, converting, and saving image data. The functions are in the utils.py function and exposed via the image.py module.

These functions can be useful convenience functions when getting started on a new deep learning computer vision project or when you need to inspect specific images.

Some of these functions are demonstrated when working with pre-trained models in the Applications section of the API documentation.

All image handling in Keras requires that the Pillow library is installed. If it is not installed, you can review the installation instructions.

Let’s take a closer look at each of these functions in turn.

Want Results with Deep Learning for Computer Vision?

Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

How to Load an Image with Keras

Keras provides the load_img() function for loading an image from file as a PIL image object.

The example below loads the Bondi Beach photograph from file as a PIL image and reports details about the loaded image.

Running the example loads the image and reports details about the loaded image.

We can confirm that the image was loaded as a PIL image in JPEG format with RGB channels and the size of 640 by 427 pixels.

The loaded image is then displayed using the default application on the workstation, in this case, the Preview application on macOS.

Example of Displaying a PIL image using the Default Application

Example of Displaying a PIL image using the Default Application

The load_img() function provides additional arguments that may be useful when loading the image, such as ‘grayscale‘ that allows the image to be loaded in grayscale (defaults to False), ‘color_mode‘ that allows the image mode or channel format to be specified (defaults to rgb), and ‘target_size‘ that allows a tuple of (height, width) to be specified, resizing the image automatically after being loaded.

How to Convert an Image With Keras

Keras provides the img_to_array() function for converting a loaded image in PIL format into a NumPy array for use with deep learning models.

The API also provides the array_to_img() function that can be used for converting a NumPy array of pixel data into a PIL image. This can be useful if the pixel data is modified while the image is in array format and can then be saved or viewed.

The example below loads the test image, converts it to a NumPy array, and then converts it back into a PIL image.

Running the example first loads the photograph in PIL format, then converts the image to a NumPy array and reports the data type and shape.

We can see that the pixel values are converted from unsigned integers to 32-bit floating point values, and in this case, converted to the array format [height, width, channels]. Finally, the image is converted back into PIL format.

How to Save Image With Keras

The Keras API also provides the save_img() function to save an image to file.

The function takes the path to save the image, and the image data in NumPy array format. The file format is inferred from the filename, but can also be specified via the ‘file_format‘ argument.

This can be useful if you have manipulated image pixel data, such as scaling, and wish to save the image for later use.

The example below loads the photograph image in grayscale format, converts it to a NumPy array, and saves it to a new file name.

Running the example first loads the image and forces the format to be grayscale.

The image is then converted to a NumPy array and saved to the new filename ‘bondi_beach_grayscale.jpg‘ in the current working directory.

To confirm that the file was saved correctly, it is loaded again as a PIL image and details of the image are reported.

The loaded grayscale image is then displayed using the default image preview application on the workstation, which in macOS is the Preview application.

Example of Saved Grayscale Image Shown Using the Default Image Viewing Application

Example of Saved Grayscale Image Shown Using the Default Image Viewing Application

Further Reading

This section provides more resources on the topic if you are looking to go deeper.

Posts

API

Summary

In this tutorial, you discovered how to use the basic image handling functions provided by the Keras API.

Specifically, you learned:

  • How to load and display an image using the Keras API.
  • How to convert a loaded image to a NumPy array and back to PIL format using the Keras API.
  • How to convert a loaded image to grayscale and save it to a new file using the Keras API.

Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.

Develop Deep Learning Models for Vision Today!

Deep Learning for Computer Vision

Develop Your Own Vision Models in Minutes

...with just a few lines of python code

Discover how in my new Ebook:
Deep Learning for Computer Vision

It provides self-study tutorials on topics like:
classification, object detection (yolo and rcnn), face recognition (vggface and facenet), data preparation and much more...

Finally Bring Deep Learning to your Vision Projects

Skip the Academics. Just Results.

See What's Inside

24 Responses to How to Load, Convert, and Save Images With the Keras API

  1. Avatar
    jessy April 17, 2019 at 8:38 pm #

    sir,
    how to see the data in the save.model.h5..how to open..tell me

    • Avatar
      Jason Brownlee April 18, 2019 at 8:26 am #

      You can load it with the load_model() function, then inspect the weights via get_weights()

  2. Avatar
    sasi August 7, 2019 at 8:36 pm #

    what we need to do for rotating images ?

  3. Avatar
    Ferdinand October 30, 2019 at 6:48 am #

    Hello sir, thank you for your explanations. I have a question: i have an RGB image (nChannels=3) which has a mode value of 16. The image is 470*430 (width*eight) and when I use img_to_array I get an array of shape (430,470,3). Do you know how can I manage to convert it to an array of shape (3,470,430)?
    The context is the following: I have imported the same image in Spark which can read my image thanks to the newly supported format “image”. However, the data is imported into one big array and I would like to know how I can manage to understand how is structured this big array. For example, raw[469] gives me the same result as x[0][0][469] but raw[470] is not the same as x[0][1][0] where raw is the image imported using spark image data format and X is the img_to_array result. By advance thank you if you can help..!
    Regards

  4. Avatar
    Dire January 23, 2020 at 8:02 pm #

    Hello sir, thank you for your information.

    i have confused for loading many data sets to train in python keras, could u explain the steps how to load my own dataset from the desk for training.

  5. Avatar
    JG June 3, 2020 at 9:48 pm #

    Hi Jason,

    Thanks.

    How I can convert an image with one single channel (let us say it is greyscale only 1 channel or even I has only a 2D array-matrix ) to an image with 3 channels (or RGB or 3D array-tensor)?. Any reference or tutorial

    I ask this question in order to be able to apply keras applications that need images or 3D arrays-tensor always 3 channels

    • Avatar
      Jason Brownlee June 4, 2020 at 6:20 am #

      You can duplicate the one channel for each of the color channels.

  6. Avatar
    JG June 5, 2020 at 2:07 am #

    OK. Thanks that it is the way I did.

    But due to keras images functions I thought there was a more elegant, quick and efficient way to do it!

  7. Avatar
    Nirbhay July 27, 2020 at 10:43 pm #

    Sir, how to load a file format of “.zip” during the image processing??

  8. Avatar
    Azri October 21, 2020 at 10:23 pm #

    Hi, is it possible to have one array for several images using img_to_array function?

  9. Avatar
    MS October 29, 2020 at 10:05 pm #

    How to convert the whole image dataset i.e all images together for cnn?

  10. Avatar
    M P SRI KAMAL TEJA March 1, 2021 at 3:36 am #

    Hi Jason,

    How to enhance the image(blurred image) using Keras API?

    • Avatar
      Jason Brownlee March 1, 2021 at 5:39 am #

      Perhaps you can model the problem as an image translation problem with a pix2pix gan?

      • Avatar
        M P SRI KAMAL TEJA March 1, 2021 at 6:04 am #

        Not sure, I’m a newbie to python. I will explore on that. BTW, can you help me with any references on image super resolution?

  11. Avatar
    Kautuk Dwivedi September 18, 2021 at 12:40 am #

    I am trying to implement cycle-gangs but I am unable to understand the structure of the dataset.
    According to the article, the dataset is to be loaded as 2 NumPy arrays. After iterating over the 2 training sets I’ve created the 2 NumPy arrays and have passed them as datasets. But then I was stuck at randint function as it accepts 2 params but you’ve given it 3 params so I removed one in that but now I am stuck.

    • Adrian Tam
      Adrian Tam September 19, 2021 at 6:18 am #

      Which code are you referring to? I don’t see randint here.

Leave a Reply