Cara menggunakan image shape python numpy

In machine learning, Python uses image data in the form of a NumPy array, i.e., [Height, Width, Channel] format. To enhance the performance of the predictive model, we must know how to load and manipulate images. In Python, we can perform one task in different ways. We have options from Numpy to Pytorch and CUDA, depending on the complexity of the problem.

By the end of this tutorial, you will have hands-on experience with:

  • Loading and displaying an image using Matplotlib, OpenCV and Keras API
  • Converting the loaded images to the NumPy array and back
  • Conducting basic manipulation of an image using the Pillow and NumPy libraries and saving it to your local system.
  • Reading images as arrays in Keras API and OpenCV

Pillow Library

Pillow is a preferred image manipulation tool. Python version 2 used Python Image Library (PIL), and Python version 3 uses Pillow Python Library, an upgrade of PIL.

You should first create a virtual environment in Anaconda for different projects. Make sure you have supporting packages like NumPy, SciPy, and Matplotlib to install in the virtual environment you create.

Once you set up the packages, you can easily install Pillow using

1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)
8.

1pip install Pillow

shell

You can confirm that the library is installed correctly by checking its version.

1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)

python

1Pillow Version: 7.0.0

Great! The latest version is now downloaded. Let's move on to the next step.

Loading the Image

Here we will learn two ways to load and get the details of an image: use Pillow library and using Matplotlib library.

Method 1: Pillow Library

Select a test image to load and work with Pillow (PIL) library. Images can be either PNG or JPEG. In this example, we'll use an image named kolala.jpeg. Either upload the image in the working directory or give your desired path. The

1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)
9 class is the heart of
1Pillow Version: 7.0.0
0
, and its properties help manipulate the pixels, format, and contrast of the image.

Cara menggunakan image shape python numpy

The

1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)
9 class uses these functions:*

  • 1Pillow Version: 7.0.0
    2: This can directly load the image. It has
    1Pillow Version: 7.0.0
    3
    properties like
    1Pillow Version: 7.0.0
    4
    , which gives information about the digital file format of an image,
    1Pillow Version: 7.0.0
    5
    size`, which displays the dimensions of the image in pixels (e.g., 480x240).

  • 1Pillow Version: 7.0.0
    6
    : This will display the image. Your default photo preview application will pop up.

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()

python

1JPEG
2(800, 450)
3RGB

Method 2: Matplotlib library

We will use the Matplotlib library to load the same image and display it in the Matplotlib frame. Just like PIL, it has the

1Pillow Version: 7.0.0
7 class that performs the same function. Functions used in this piece of code are
1Pillow Version: 7.0.0
8
, which loads the image in the form of an array of the pixel and
1Pillow Version: 7.0.0
9
, which displays that image.

We will use the

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
0 class from the Matplotlib library to plot the image into the frame.

1# load and display an image with Matplotlib
2from matplotlib import image
3from matplotlib import pyplot
4# load image as pixel array
5image = image.imread('kolala.jpeg')
6# summarize shape of the pixel array
7print(image.dtype)
8print(image.shape)
9# display the array of pixels as an image
10pyplot.imshow(image)
11pyplot.show()

python

After the first step of loading the image using the

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
1 argument, we get a report on the data type of the array. In this case, it is 8-bit unsigned integers. The shape of the array is 800 pixels wide by 450 pixels high and
1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
2
denotes color channels for red, green, and blue.

Cara menggunakan image shape python numpy

Convert to NumPy Array and Back

In Python, Pillow is the most popular and standard library when it comes to working with image data.

NumPy uses the

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
3 class to convert PIL images into NumPy arrays. The
1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
4
function also produce the same result. The
1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
5
function displays the class of an image.

The process can be reversed using the

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
6 function. This function comes in handy when the manipulation is performed on
1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
7
image data, that we laterwant to save as a PNG or JPEG file.

1from PIL import Image
2from numpy import asarray
3# load the image
4image = Image.open('kolala.jpeg')
5# convert image to numpy array
6data = asarray(image)
7print(type(data))
8# summarize shape
9print(data.shape)
10
11# create Pillow image
12image2 = Image.fromarray(data)
13print(type(image2))
14
15# summarize image details
16print(image2.mode)
17print(image2.size)

python

Cara menggunakan image shape python numpy

1# load and show an image with Pillow
2from PIL import Image
3# Open the image form working directory
4image = Image.open('kolala.jpeg')
5# summarize some details about the image
6print(image.format)
7print(image.size)
8print(image.mode)
9# show the image
10load_image.show()
8 gives the value of each pixel of the NumPy array image.

1print(data)

python

Cara menggunakan image shape python numpy

Manipulating and Saving the Image

Now that we have converted our image into a Numpy array, we might come across a case where we need to do some manipulations on an image before using it into the desired model. In this section, you will be able to build a grayscale converter. You can also resize the array of the pixel image and trim it.

After performing the manipulations, it is important to save the image before performing further steps. The

1Pillow Version: 7.0.0
4 argument saves the file in different formats, such as PNG, GIF, or PEG.

For example, the code below loads the photograph in JPEG format and saves it in PNG format.

Converting Images to Grayscale

1import numpy as np
2from PIL import Image
3
4im = np.array(Image.open('kolala.jpeg').convert('L')) #you can pass multiple arguments in single line
5print(type(im))
6
7gr_im= Image.fromarray(im).save('gr_kolala.png')

python

1<class 'numpy.ndarray'>

Cara menggunakan image shape python numpy

Resizing the Image

1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)
0

python

1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)
1

Trimming the Image

1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)
2

python

Cara menggunakan image shape python numpy

Check for the images in the path you have mentioned.

Keras API

Let's consider the same test image. Keras provides the functions for loading, converting, and saving image data. To install Keras API in an Anaconda virtual environment, use the

1JPEG
2(800, 450)
3RGB
0 command (CPU version). Keras runs on the top of the TensorFlow framework. Make sure the package is correctly installed.

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

Loading an Image With Keras API

Keras provides the

1JPEG
2(800, 450)
3RGB
1 function for loading a PIL image. Learn more about the function here .

1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)
3

python

According to the output below, we can confirm that the loaded image is in PIL format and has JPEG format RGB color channels and a size of 800 x 450 pixels. The image will be displayed in your default image viewer.

Cara menggunakan image shape python numpy

Converting an Image With Keras API

Keras uses the

1JPEG
2(800, 450)
3RGB
2 function to convert the PIL image into numpy. The API also provides the
1JPEG
2(800, 450)
3RGB
3
function, which can be used for converting an array of pixel data into a PIL image.

1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)
4

python

Cara menggunakan image shape python numpy

Saving an Image with Keras

The Keras API uses the

1JPEG
2(800, 450)
3RGB
4 function to save an image locally. Here the function takes the path and the file name where we want to save the image data in NumPy array format. This function is useful when you have manipulated the image and wish to save the image for later use.

1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)
5

python

Cara menggunakan image shape python numpy

OpenCV Library

OpenCV is a library that performs traditional computer vision algorithms. The Tensorflow + Keras framework is a go-to option for anyone who wants to work with deep learning. OpenCV version 3.x has introduced DNN and Caffe frameworks to solve deep learning problems .

To work with an OpenCV library, you need to install it in the virtual environment using

1JPEG
2(800, 450)
3RGB
5. The
1JPEG
2(800, 450)
3RGB
6
package provides an
1Pillow Version: 7.0.0
8
function to load the image. It also reads a PIL image in the NumPy array format. The only thing we need to convert is the image color from BGR to RGB.
1JPEG
2(800, 450)
3RGB
8
saves the image in the file.

1# check Pillow version number
2import PIL
3print('Pillow Version:', PIL.__version__)
6

python

1<class 'numpy.ndarray'>

Conclusion

Python is a flexible tool, giving us a choice to load a PIL image in two different ways. In this guide, you learned some manipulation tricks on a Numpy Array image, then converted it back to a PIL image and saved our work. This guide also gave you a heads up on converting images into an array form by using Keras API and OpenCV library. Further, you can follow the Pillow library documentation link and try performing different manipulation techniques, such as building a function to expand the image data and feed into deep learning neural networks.