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?
 Convert an PIL image to a NumPy Array Using pillow library and np.array()
 Convert an PIL image to a NumPy Array using pillow library and np.asarray()
 Convert an PIL image to a NumPy Array using imread() from matplotlib.pyplot
 Convert an PIL image to a NumPy Array using imread() method from imageio module
 Convert an PIL image to a NumPy Array using imread() method from opencv module
 Summary
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
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 :
 Import PIL library and NumPy library.
 Create numpy Image object using open() method
 Pass this Image object to the array() method, This will return a numpy array
 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 :
 Import PIL library and NumPy library.
 Create numpy Image object using open() method
 Pass this Image object to the asarray() method, This will return a numpy array
 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 :
 Import numpy library and matplotlib library.
 Pass this Image path to the imread() method.
 The imread() method will return an numpy array, which is an image data
 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 :
 Import numpy library and imageio library.
 Pass the Image path to the imread() method.
 The imread() method will return an numpy array, which is an image data
 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 :
 Import numpy library and cv2 library.
 Pass the Image path to the imread() method.
 The imread() method will return an numpy array, which is an image data
 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

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
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 soughtafter 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.