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:
- Test Image
- Keras Image Processing API
- How to Load an Image With Keras
- Convert Image an With Keras
- 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.
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.
1 2 3 4 5 6 7 8 9 10 11 |
# example of loading an image with the Keras API from keras.preprocessing.image import load_img # load the image img = load_img('bondi_beach.jpg') # report details about the image print(type(img)) print(img.format) print(img.mode) print(img.size) # show the image img.show() |
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.
1 2 3 4 |
<class 'PIL.JpegImagePlugin.JpegImageFile'> JPEG RGB (640, 427) |
The loaded image is then displayed using the default application on the workstation, in this case, the Preview application on macOS.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# example of converting an image with the Keras API from keras.preprocessing.image import load_img from keras.preprocessing.image import img_to_array from keras.preprocessing.image import array_to_img # load the image img = load_img('bondi_beach.jpg') print(type(img)) # convert to numpy array img_array = img_to_array(img) print(img_array.dtype) print(img_array.shape) # convert back to image img_pil = array_to_img(img_array) print(type(img)) |
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.
1 2 3 4 |
<class 'PIL.JpegImagePlugin.JpegImageFile'> float32 (427, 640, 3) <class 'PIL.JpegImagePlugin.JpegImageFile'> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# example of saving an image with the Keras API from keras.preprocessing.image import load_img from keras.preprocessing.image import save_img from keras.preprocessing.image import img_to_array # load image as as grayscale img = load_img('bondi_beach.jpg', grayscale=True) # convert image to a numpy array img_array = img_to_array(img) # save the image with a new filename save_img('bondi_beach_grayscale.jpg', img_array) # load the image to confirm it was saved correctly img = load_img('bondi_beach_grayscale.jpg') print(type(img)) print(img.format) print(img.mode) print(img.size) img.show() |
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.
1 2 3 4 |
<class 'PIL.Image.Image'> None RGB (640, 427) |
The loaded grayscale image is then displayed using the default image preview application on the workstation, which in macOS is the Preview application.
Further Reading
This section provides more resources on the topic if you are looking to go deeper.
Posts
API
- Pillow Library
- Keras Image Preprocessing API
- Keras Applications API
- Keras image.py Source Code
- Keras utils.py Source Code
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.
sir,
how to see the data in the save.model.h5..how to open..tell me
You can load it with the load_model() function, then inspect the weights via get_weights()
what we need to do for rotating images ?
I give examples here:
https://machinelearningmastery.com/how-to-load-and-manipulate-images-for-deep-learning-in-python-with-pil-pillow/
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
Great question!
Yes, I show how here:
https://machinelearningmastery.com/a-gentle-introduction-to-channels-first-and-channels-last-image-formats-for-deep-learning/
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.
See this tutorial:
https://machinelearningmastery.com/load-machine-learning-data-python/
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
You can duplicate the one channel for each of the color channels.
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!
There may be, I don’t recall off hand, sorry.
Sir, how to load a file format of “.zip” during the image processing??
Unzip it, then load the image.
Hi, is it possible to have one array for several images using img_to_array function?
I don’ think so, perhaps try it and see.
How to convert the whole image dataset i.e all images together for cnn?
If you don’t have many images, you can load them all into memory and prepare them, then save the result.
If you have many images, you can prepare them just in time:
https://machinelearningmastery.com/how-to-load-large-datasets-from-directories-for-deep-learning-with-keras/
Hi Jason,
How to enhance the image(blurred image) using Keras API?
Perhaps you can model the problem as an image translation problem with a pix2pix gan?
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?
Not off hand, sorry.
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.
Which code are you referring to? I don’t see randint here.