How to Convert images to NumPy array?

Images are an easier way to represent the working model. In Machine Learning, Python uses the image data in the format of Height, Width, Channel format. i.e. Images are converted into Numpy Array in Height, Width, Channel format. 

Modules Needed:

  • NumPy: By default in higher versions of Python like 3.x onwards, NumPy is available and if not  available(in lower versions), one can install by using
pip install numpy
  • Pillow: This has to be explicitly installed in later versions too. It is a preferred image manipulation tool. In Python 3, Pillow python library which is nothing but the upgradation of PIL only. It can be installed using
pip install Pillow

One can easily check the version of installed Pillow by using the below code

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import PIL
  
print('Installed Pillow Version:', PIL.__version__)

chevron_right


Output:

Installed Pillow Version: 7.2.0

Loading the images via Pillow Library

Let us check for an image that is in the PNG or JPEG format. The image can be referred via its path. Image class is the heart of PIL. It has open() function which opens up an image and digital file format can be retrieved as well as pixel format.



Image Used:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from PIL import Image
  
  
# sample.png is the name of the image
# file and assuming that it is uploaded
# in the current directory or we need
# to give the path
image = Image.open('Sample.png')
  
# summarize some details about the image
print(image.format)
print(image.size)
print(image.mode)

chevron_right


Output :

PNG
(400, 200)
RGB

Converting an image into NumPy Array

Python provides many modules and API’s for converting an image into a NumPy array. Let’s discuss a few of them in detail.

Using NumPy module

Numpy module in itself provides various methods to do the same. These methods are – 

Method 1: Using asarray() function

asarray() function is used to convert PIL images into NumPy arrays. This function converts the input to an array



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import the necessary libraries
from PIL import Image
from numpy import asarray
  
  
# load the image and convert into
# numpy array
img = Image.open('Sample.png')
  
# asarray() class is used to convert
# PIL images into NumPy arrays
numpydata = asarray(img)
  
# <class 'numpy.ndarray'>
print(type(numpydata))
  
#  shape
print(numpydata.shape)

chevron_right


Output :

<class 'numpy.ndarray'>
(200, 400, 3)

Method 2: Using numpy.array() function

By using numpy.array() function which takes an image as the argument and converts to NumPy array

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from PIL import Image
import numpy
  
  
img= Image.open("Sample.png")
np_img = numpy.array(img)
  
print(np_img.shape)

chevron_right


Output :

(200, 400, 3)

In order to get the value of each pixel of the NumPy array image, we need to print the retrieved data that got either from asarray() function or array() function.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import the necessary libraries
from PIL import Image
from numpy import asarray
  
  
# load the image and convert into 
# numpy array
img = Image.open('Sample.png')
numpydata = asarray(img)
  
# data
print(numpydata)

chevron_right


Output :

[[[111  60   0]
 [116  65   0]
 [122  69   0]
 ...
 [ 97  47   0]
 [ 99  47   0]
 [100  49   0]]
[[111  61   0]
 [118  65   0]
 [122  69   0]
 ...
 [ 97  47   0]
 [ 99  48   0]
 [100  49   0]]
[[118  65   0]
 [122  69   0]
 [126  73   3]
 ...
 [ 98  48   0]
 [100  49   0]
 [100  49   0]]
...
[[ 96  44   7]
 [ 95  43   6]
 [ 93  41   4]
 ...
 [225  80   3]
 [228  80   0]
 [229  78   0]]
[[ 93  40   6]
 [ 90  37   5]
 [ 85  32   0]
 ...
 [226  81   4]
 [231  80   1]
 [232  79   1]]
[[ 89  36   4]
 [ 84  31   0]
 [ 79  26   0]
 ...
 [228  81   4]
 [232  81   4]
 [233  80   2]]]

Getting back the image from converted Numpy Array

Image.fromarray() function helps to get back the image from converted numpy array. We get back the pixels also same after converting back and forth. Hence, this is very much efficient



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import the necessary libraries
from PIL import Image
from numpy import asarray
  
  
# load the image and convert into 
# numpy array
img = Image.open('Sample.png')
numpydata = asarray(img)
  
print(type(numpydata))
  
#  shape
print(numpydata.shape)
  
# Below is the way of creating Pillow 
# image from our numpyarray
pilImage = Image.fromarray(numpydata)
print(type(pilImage))
  
# Let us check  image details
print(pilImage.mode)
print(pilImage.size)

chevron_right


Output :

<class 'numpy.ndarray'>
(200, 400, 3)
<class 'PIL.Image.Image'>
RGB
(400, 200)

Converting Images using Keras API

Keras API provides the functions for loading, converting, and saving image data. Keras is possible to run on the top of the TensorFlow framework and hence that is mandatory to have. Deep learning computer vision images require Keras API. To install it type the below command in the terminal 

pip install keras

As Keras requires TensorFlow 2.2 or higher. If not there, need to install it. To install it type the below command in the terminal.

pip install tensorflow

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from keras.preprocessing.image import load_img
import warnings
  
# load the image via load_img 
# function
img = load_img('sample.png')
  
# details about the image printed below
print(type(img)) 
print(img.format)
print(img.mode)
print(img.size)

chevron_right


Output :

<class 'PIL.PngImagePlugin.PngImageFile'>
PNG
RGB
(400, 200)

Using Keras API, convert images to Numpy Array and reverting the image from Numpy Array

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from keras.preprocessing.image import load_img
import warnings
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import array_to_img
  
  
# load the image via load_img function
img = load_img('sample.png')
  
# details about the image printed below
print(type(img))
print(img.format)
print(img.mode)
print(img.size)
  
# convert the given image into  numpy array
img_numpy_array = img_to_array(img)
print("Image is converted and NumPy array information :")
  
# <class 'numpy.ndarray'>
print(type(img_numpy_array))
  
# type: float32
print("type:", img_numpy_array.dtype)
  
# shape: (200, 400, 3)
print("shape:", img_numpy_array.shape)
  
# convert back to image
img_pil_from_numpy_array = array_to_img(img_numpy_array)
  
# <class 'PIL.PngImagePlugin.PngImageFile'>
print("converting NumPy array into image:",
      type(img_pil_from_numpy_array))

chevron_right


Output :

<class 'PIL.PngImagePlugin.PngImageFile'>
PNG
RGB
(400, 200)
Image is converted and NumPy array information :
<class 'numpy.ndarray'>
type: float32
shape: (200, 400, 3)
converting NumPy array into image: <class 'PIL.Image.Image'>

From the above output, we can check that the source image PIL.Image.Image and destination image types are the same.



Using OpenCV Library

OpenCV version from 3.x has DNN and Caffe frameworks, and they are very helpful to solve deep learning problems. It can be installed by using

pip install opencv-contrib-python

cv2 package has the following methods

  • imread() function is used to load the image and It also reads the given image (PIL image) in the NumPy array format. 
  • Then we need to convert the image color from BGR to RGB. 
  • imwrite() is used to save the image in the file.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import cv2
  
image = cv2.imread('Sample.png')
  
# BGR -> RGB
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  
cv2.imwrite('opncv_sample.png', img) 
print (type(img))

chevron_right


Output :

<class 'numpy.ndarray'>

Conclusion

Python is a very flexible tool and we have seen ways of converting images into Numpy Array and similarly back to images using different APIs. Manipulating the converted array and forming different image data and one can feed into deep learning neural networks.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Freelancer in Software projects using Java, Python, SQL,MongoDB technologies Can able to quickly adapt to any new technologies and can provide guidance in software projects

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.