How to Convert images to NumPy array?

29 Aug, 2020
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. 
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
import PIL  print('Installed Pillow Version:', PIL.__version__)
Output:
Installed Pillow Version: 7.2.0

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
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 pathimage = Image.open('Sample.png')  # summarize some details about the imageprint(image.format)print(image.size)print(image.mode)
Output :
PNG
(400, 200)
RGB
Python provides many modules and API’s for converting an image into a NumPy array. Let’s discuss a few of them in detail.
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
# Import the necessary librariesfrom PIL import Imagefrom numpy import asarray    # load the image and convert into# numpy arrayimg = Image.open('Sample.png')  # asarray() class is used to convert# PIL images into NumPy arraysnumpydata = asarray(img)  # <class 'numpy.ndarray'>print(type(numpydata))  #  shapeprint(numpydata.shape)
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
from PIL import Imageimport numpy    img= Image.open("Sample.png")np_img = numpy.array(img)  print(np_img.shape)
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
# Import the necessary librariesfrom PIL import Imagefrom numpy import asarray    # load the image and convert into # numpy arrayimg = Image.open('Sample.png')numpydata = asarray(img)  # dataprint(numpydata)
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]]]
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
# Import the necessary librariesfrom PIL import Imagefrom numpy import asarray    # load the image and convert into # numpy arrayimg = Image.open('Sample.png')numpydata = asarray(img)  print(type(numpydata))  #  shapeprint(numpydata.shape)  # Below is the way of creating Pillow # image from our numpyarraypilImage = Image.fromarray(numpydata)print(type(pilImage))  # Let us check  image detailsprint(pilImage.mode)print(pilImage.size)
Output :
<class 'numpy.ndarray'>
(200, 400, 3)
<class 'PIL.Image.Image'>
RGB
(400, 200)
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
from keras.preprocessing.image import load_imgimport warnings  # load the image via load_img # functionimg = load_img('sample.png')  # details about the image printed belowprint(type(img)) print(img.format)print(img.mode)print(img.size)
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
from keras.preprocessing.image import load_imgimport warningsfrom keras.preprocessing.image import img_to_arrayfrom keras.preprocessing.image import array_to_img    # load the image via load_img functionimg = load_img('sample.png')  # details about the image printed belowprint(type(img))print(img.format)print(img.mode)print(img.size)  # convert the given image into  numpy arrayimg_numpy_array = img_to_array(img)print("Image is converted and NumPy array information :")  # <class 'numpy.ndarray'>print(type(img_numpy_array))  # type: float32print("type:", img_numpy_array.dtype)  # shape: (200, 400, 3)print("shape:", img_numpy_array.shape)  # convert back to imageimg_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))
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.
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
import cv2  image = cv2.imread('Sample.png')  # BGR -> RGBimg = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  cv2.imwrite('opncv_sample.png', img) print (type(img))
Output :
<class 'numpy.ndarray'>
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.
Image-Processing
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.