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.

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.

Download Your FREE Mini-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.

Click to learn more.


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

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

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

    • 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()

Leave a Reply