Convert an PIL image to a NumPy Array in Python

In this article, we will learn how to convert an image to a NumPy Array in Python. Given an Image we need to convert it into NumPy Array in Python.

Table Of Contents

How the images are stored in a computer?

The usual black and white Images are represented using a matrix, where each cell in the matrix represent a pixel. The pixel color is either black or which, The value in the cell represent the intensity of the color, like 0 is for black and 255 is for white, So color intensity changes with The numbers in cell.

Any color we see can be derived from the RGB (red, blue, green), by changing the intensity of each color we can produce diffrent colors. To store a color image, the rgb values of a image pixels are stored in a 3d array, in other words it is a combination of 3 2d arrays. These three 2d arrays have pixel values of red, blue and green respectivly.

There are multiple ways to convert an Image to a NumPy Array in Python. Lets discuss all the methods one by one with proper approach and a working code example

Advertisements

Convert an PIL image to a NumPy Array Using pillow library and np.array()

The pillow library has Image module, and it provide open() method. The open() method will return an image object and we can convert it into numpy array using the numpy.array() method.

Syntax of open()

PIL.Image.open(fp)
  • Parameters:
  • fp = Name or path of the image file to be opened.
  • Returns:
  • An Image object

Approach :

  1. Import PIL library and NumPy library.
  2. Create numpy Image object using open() method
  3. Pass this Image object to the array() method, This will return a numpy array
  4. Print the numpy array

Source Code :

from PIL import Image
import numpy as np

# opening the image
img = Image.open("image.jpg")

#Converting the image object into numpy array.
imgArray = np.array(img)

#printing the numpy array.
print(imgArray)

Output :

[[[ 8 24 49]
  [ 8 24 49]
  [ 9 25 50]
  ...
  [ 6 24 46]
  [ 8 26 48]
  [ 9 27 49]]

 [[ 8 24 49]
  [ 8 24 49]
  [ 9 25 50]
  ...
  [ 6 24 46]
  [ 8 26 48]
  [ 8 26 48]]

 [[ 8 24 49]
  [ 8 24 49]
  [ 9 25 50]
  ...
  [ 6 24 46]
  [ 7 25 47]
  [ 8 26 48]]

 ...

 [[ 1  9 28]
  [ 2 10 29]
  [ 3 11 30]
  ...
  [18 16 29]
  [17 15 28]
  [20 18 31]]

 [[ 3 11 30]
  [ 3 11 30]
  [ 4 12 31]
  ...
  [19 17 30]
  [19 17 30]
  [23 21 34]]

 [[ 5 13 32]
  [ 5 13 32]
  [ 5 13 32]
  ...
  [20 18 31]
  [22 20 33]
  [27 25 38]]]

Convert an PIL image to a NumPy Array using pillow library and np.asarray()

The pillow library has Image module and this Image module provide open() method. It returns an image object and we can convert it into numpy array using the numpy.asarray() method.

Syntax of open()

PIL.Image.open(fp)
  • Parameters:
  • fp = Name or path of the image file to be opened.
  • Returns:
  • An Image object

Approach :

  1. Import PIL library and NumPy library.
  2. Create numpy Image object using open() method
  3. Pass this Image object to the asarray() method, This will return a numpy array
  4. Print the numpy array

Source Code :

from PIL import Image
import numpy as np

# opening the image
img = Image.open("image.jpg")

#Converting the image object into numpy array.
imgArray = np.asarray(img)

#printing the numpy array.
print(imgArray)

Output :

[[[ 8 24 49]
  [ 8 24 49]
  [ 9 25 50]
  ...
  [ 6 24 46]
  [ 8 26 48]
  [ 9 27 49]]

 [[ 8 24 49]
  [ 8 24 49]
  [ 9 25 50]
  ...
  [ 6 24 46]
  [ 8 26 48]
  [ 8 26 48]]

 [[ 8 24 49]
  [ 8 24 49]
  [ 9 25 50]
  ...
  [ 6 24 46]
  [ 7 25 47]
  [ 8 26 48]]

 ...

 [[ 1  9 28]
  [ 2 10 29]
  [ 3 11 30]
  ...
  [18 16 29]
  [17 15 28]
  [20 18 31]]

 [[ 3 11 30]
  [ 3 11 30]
  [ 4 12 31]
  ...
  [19 17 30]
  [19 17 30]
  [23 21 34]]

 [[ 5 13 32]
  [ 5 13 32]
  [ 5 13 32]
  ...
  [20 18 31]
  [22 20 33]
  [27 25 38]]]

Convert an PIL image to a NumPy Array using imread() from matplotlib.pyplot

The matplotlib.pyplot module provide imread() method. The imread() method will convert the Image into Numpy array.

Syntax of imread()

matplotlib.pyplot.imread(fp)

  • Parameters:
  • fp = Name or path to read the image from.
  • Returns:
  • Return an image data, it is a numpy array.

Approach :

  1. Import numpy library and matplotlib library.
  2. Pass this Image path to the imread() method.
  3. The imread() method will return an numpy array, which is an image data
  4. Print the numpy array

Source Code :

import matplotlib.pyplot as mp
import numpy as np

# reading the numpy array from the image.
imgArray = mp.imread("image.jpg")

# printing the numpy array.
print(imgArray)

Output :

[[[ 8 24 49]
  [ 8 24 49]
  [ 9 25 50]
  ...
  [ 6 24 46]
  [ 8 26 48]
  [ 9 27 49]]

 [[ 8 24 49]
  [ 8 24 49]
  [ 9 25 50]
  ...
  [ 6 24 46]
  [ 8 26 48]
  [ 8 26 48]]

 [[ 8 24 49]
  [ 8 24 49]
  [ 9 25 50]
  ...
  [ 6 24 46]
  [ 7 25 47]
  [ 8 26 48]]

 ...

 [[ 1  9 28]
  [ 2 10 29]
  [ 3 11 30]
  ...
  [18 16 29]
  [17 15 28]
  [20 18 31]]

 [[ 3 11 30]
  [ 3 11 30]
  [ 4 12 31]
  ...
  [19 17 30]
  [19 17 30]
  [23 21 34]]

 [[ 5 13 32]
  [ 5 13 32]
  [ 5 13 32]
  ...
  [20 18 31]
  [22 20 33]
  [27 25 38]]]

Convert an PIL image to a NumPy Array using imread() method from imageio module

The imageio module provide imread() method. It can convert an Image into Numpy array.

Syntax of imread()

imageio.imread(fp)
  • Parameters:
  • fp = Name or path to read the image from.
  • Returns:
  • Return an image data, it is a numpy array.

Approach :

  1. Import numpy library and imageio library.
  2. Pass the Image path to the imread() method.
  3. The imread() method will return an numpy array, which is an image data
  4. Print the numpy array

Source Code :

import imageio
import numpy as np

# reading the numpy array from the image.
imgArray = imageio.imread('image.jpg')

# printing the numpy array.
print(imgArray)

Output :

[[[ 8 24 49]
  [ 8 24 49]
  [ 9 25 50]
  ...
  [ 6 24 46]
  [ 8 26 48]
  [ 9 27 49]]

 [[ 8 24 49]
  [ 8 24 49]
  [ 9 25 50]
  ...
  [ 6 24 46]
  [ 8 26 48]
  [ 8 26 48]]

 [[ 8 24 49]
  [ 8 24 49]
  [ 9 25 50]
  ...
  [ 6 24 46]
  [ 7 25 47]
  [ 8 26 48]]

 ...

 [[ 1  9 28]
  [ 2 10 29]
  [ 3 11 30]
  ...
  [18 16 29]
  [17 15 28]
  [20 18 31]]

 [[ 3 11 30]
  [ 3 11 30]
  [ 4 12 31]
  ...
  [19 17 30]
  [19 17 30]
  [23 21 34]]

 [[ 5 13 32]
  [ 5 13 32]
  [ 5 13 32]
  ...
  [20 18 31]
  [22 20 33]
  [27 25 38]]]

Convert an PIL image to a NumPy Array using imread() method from opencv module

The opencv module provide imread() method. It can convert the Image into Numpy array.

Syntax of imread()

cv2.imread(fp)
  • Parameters:
  • fp = Name or path to read the image.
  • Returns:
  • numpy array

Approach :

  1. Import numpy library and cv2 library.
  2. Pass the Image path to the imread() method.
  3. The imread() method will return an numpy array, which is an image data
  4. Print the numpy array

Source Code :

import cv2   
import numpy as np

# reading the numpy array from the image.
imgArray = cv2.imread('image.jpg')

# printing the numpy array.
print(imgArray)

Output :

[[[49 24  8]
  [49 24  8]
  [50 25  9]
  ...
  [46 24  6]
  [48 26  8]
  [49 27  9]]

 [[49 24  8]
  [49 24  8]
  [50 25  9]
  ...
  [46 24  6]
  [48 26  8]
  [48 26  8]]

 [[49 24  8]
  [49 24  8]
  [50 25  9]
  ...
  [46 24  6]
  [47 25  7]
  [48 26  8]]

 ...

 [[28  9  1]
  [29 10  2]
  [30 11  3]
  ...
  [29 16 18]
  [28 15 17]
  [31 18 20]]

 [[30 11  3]
  [30 11  3]
  [31 12  4]
  ...
  [30 17 19]
  [30 17 19]
  [34 21 23]]

 [[32 13  5]
  [32 13  5]
  [32 13  5]
  ...
  [31 18 20]
  [33 20 22]
  [38 25 27]]]

Summary

Great! you made it, We have discussed All possible methods to convert a image into NumPy Array in Python. Happy learning.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top