Change the color of the Image
To convert the colorful images into grayscale, just set grayscale = True in load_img() method. To save the converted images we need to convert the image into an array, otherwise the save_img() method will throw an error. After converting the image into grayscale image we see it still shows mode RGB, and the size is the same.
Python3
from keras.preprocessing.image import save_img from keras.preprocessing.image import img_to_array # load image as grayscale img = load_img( 'dog.jpg' , grayscale = True ) # convert image to a numpy array img_array = img_to_array(img) # save the image with a new filename save_img( 'dog_grayscale.jpg' , img_array) # load the image to confirm it was saved # correctly img = load_img( 'dog_grayscale.jpg' ) print ( type (img)) print (img. format ) print (img.mode) print (img.size) img.show() |
Output:
Image Processing with Keras in Python
In this article, we are doing Image Processing with Keras in Python. Keras API is a deep learning library that provides methods to load, prepare and process images.
We will cover the following points in this article:
- Load an image
- Process an image
- Convert Image into an array and vice-versa
- Change the color of the image
- Process image dataset