Convert PIL image to NumPy array in Python

You will learn how to convert the PIL image to a NumPy array in this Python tutorial. As part of Python programming, PIL stands for Python Imaging Library, an image-related Python interpreter library that handles and executes operations on images.

Modules Required:

Here in this PIL image conversion program, there are two main libraries named Pillow and NumPy, make sure to install these libraries in your code space extension. Due to its limited compatibility with Python 2, PIL was halted in 2011 and was replaced with Pillow, which supports Python 3.

Below are the library installation instructions.

#NumPy library
pip install numpy
#Pillow library
pip install pillow

The PIL relies heavily on its image class. In the first step kindly examine the type of the image for example PNG or JPEG. An image can be opened up in digital and pixel formats using the open() function.

The NumPy uses various methods to convert the PIL images into NumPy array. Some of them are explained below:

First method: By using asarray() function

Arrays can be converted from PIL images into NumPy arrays with the asarray() function in Python. Firstly, import all the important libraries required into the code space then copy the name of the sample image with the open() function and convert it into NumPy arrays then with the help of using class ‘numpy.ndarray‘ print the type and shape of the given image sample.

# Import all the required libraries
from PIL import Image
from numpy import asarray
# copy the name of the sample image
img = Image.open('sample.jpg')
 
#numpy arrays is converted from PIL images using asarray()class.

numpydata = asarray(img)
 
# <class 'numpy.ndarray'>
print(type(numpydata))
 
#  shape
print(numpydata.shape)

Output:

<class 'numpy.ndarray'>
(703, 998, 3)

Second Method: By using numpy.array() function – PIL image to NumPy array

This is another method for converting the PIL image to the NumPy arrays by taking an image as an argument. In this particular method first import all the important libraries and with help of open function place the name of the sample image and at last print the shape of the image. Look at the code below for reference.

#import the important libraries
from PIL import Image
import numpy

#load the path in the given open function
img= Image.open("sample.jpg")
np_img = numpy.array(img)
#print the shape of the sample image
print(np_img.shape)

Output:

(703, 998, 3)

With the help of array() or asarray() functions in NumPy, we can obtain the value of all the pixels of the array image. For reference, here is the code given below.

# Import the all required libraries
from PIL import Image
from numpy import asarray
 
 
# copy the path of the image using open function
img = Image.open('sample.jpg')
numpydata = asarray(img)
 
# print the data
print(numpydata)

Output:

[[[101 64 108]
[101 64 108]
[101 64 108]
...
[124 111 183]
[124 111 183]
[124 111 183]]

[[101 64 108]
[101 64 108]
[101 64 108]
...
[124 111 183]
[124 111 183]
[124 111 183]]

[[101 64 108]
[101 64 108]
[101 64 108]
...
[124 111 183]
[124 111 183]
[124 111 183]]

...

[[ 63 39 65]
[ 63 39 65]
[ 63 39 65]
...
[ 24 19 51]
[ 24 19 51]
[ 24 19 51]]

[[ 63 39 65]
[ 63 39 65]
[ 63 39 65]
...
[ 24 19 51]
[ 24 19 51]
[ 24 19 51]]

[[ 63 39 65]
[ 63 39 65]
[ 63 39 65]
...
[ 24 19 51]
[ 24 19 51]
[ 24 19 51]]]

 

Leave a Reply

Your email address will not be published. Required fields are marked *