Resize an Image

We can perform certain functions on the image like resizing it, changing its color, convert into an array, etc before training any model. To resize the shape of the image, resize() method is invoked on the image. The size in which we want to convert the image should be iterable. 

Python3




img = load_img('dog.jpg')
 
# change image size
image = img.resize([30, 30])
 
# print new image size
print(image.size)


Output: 

(30, 30)

We can see in the output, 30 x 30 is the new size of the image.

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

Similar Reads

Load the Image

In Keras, load_img() function is used to load image. The image loaded using load_img() method is PIL object. Certain information can be accessed from loaded images like image type which is PIL object, the format is JPEG, size is (6000,4000), mode is RGB, etc. We are using dog images throughout the article....

Resize an Image

...

Convert image into an array

We can perform certain functions on the image like resizing it, changing its color, convert into an array, etc before training any model. To resize the shape of the image, resize() method is invoked on the image. The size in which we want to convert the image should be iterable....

Change the color of the Image

...

Process an Image dataset

There is img_to_array() method to convert images into array, and array_to_img() method to convert image array back to image. In the below example, we are just accessing the 0th index of the image array. We can get information like image array shape, type, etc. When the image array is converted to an image, it again is a PIL object....