How to Convert Images to Numpy Arrays in Python

Convert Image File To Float Array In Python

In order to train machine learning models on the various dimensions of an image, we need to convert it into a numpy array. Implicitly this is always done, but there are ways to do the same explicitly as well. It can be used for carrying out complex calculations with the speed of light.

Libraries such as OpenCV store images in the form of three dimensional numpy arrays for lightning fast operations. Sometimes libraries such as PIL, keras and scikit-image wrap pictures in special formats for easier image manipulation.

Since there are no direct numpy in built functions to convert an image to a numpy array, hence we need external tools such as OpenCV, Keras,Scikit-image or PIL.

We’ll look at all of the above methods one by one. We can also convert an numpy array back to an image using python as well.

Setting Up The Required Libraries

You have to have the respective modules in your local system before carrying out the code in the following sections. Run the following code to install or update the libraries in your system from your command prompt, preferably in the administrator mode.

pip install numpy
pip install Pillow
pip install tensorflow
pip install keras
pip install scikit-image
pip install opencv-python

Now with that out of the way, we will look at the implementation of all the above mentioned packages for image conversion into a numpy array one by one.

The image that I will use is given below, you can save this image or you can use whatever image you want.

Sample
Sample.png

Suggested: Python TensorFlow – A Beginner’s Introduction.

Method 1 – Converting Images with OpenCV

OpenCV is the open source computer vision library that is used for real time computer vision and image manipulation in python. It is a free and open source library and supports execution of models for training and other machine learning algorithms.

The imread() function and the imwrite() function are the two most widely used OpenCV library functions that are used to read an image and save it as a specified file respectively.

#importing the opencv module in the code
import cv2
#storing the image in a variable
original = cv2.imread('/content/Sample.png')
 
#transforming from BGR to RGB
convert = cv2.cvtColor(original, cv2.COLOR_BGR2RGB)
#writing the image to a file 
cv2.imwrite('opncv_sample.png', convert)
#printing the type of the converted image
print ("The image has been converted from .png to ",type(convert))

The output would generate another image called “convert.png” which will have the RGB converted image and the following would be shown in the terminal:

The image has been converted from .png to  <class 'numpy.ndarray'>
Using OpenCV
Using OpenCV

Method 2 – Image Conversion Using Pillow (PIL)

The pillow library is one of the most powerful image manipulating libraries in python. It is used for storing an image in various formats as per use. We can use it in addition to the asarray() function in order to convert and store the picture as an array.

PIL can be used to convert an image to an array very easily. Let’s take a look at it’s code:

# Importing the required modules
from PIL import Image
from numpy import asarray 
#loading the image 
original = Image.open('/content/Sample.png')
# using the asarray() function
# converting PIL images into NumPy arrays
numpydataarr = asarray(original) 
# printing the type of the image after conversion
print("The type after conversion is:",type(numpydataarr))
# displaying the shape of the image
print("dimensions of the array", numpydataarr.shape)
#Printing the pixel information
print("The pixel information is=")
print(numpydataarr) 

The output will be longer than usual because this program will display the entire information about the pixels of the image.

The type after conversion is: <class 'numpy.ndarray'>
dimensions of the array (600, 1200, 4)
The pixel information is=
[[[201 215 224 255]
  [201 215 224 255]
  [201 215 224 255]
  ...
  [190 191 193 255]
  [149 149 152 255]
  [ 68  69  72 255]]

 [[201 215 224 255]
  [201 215 224 255]
  [201 215 224 255]
  ...
  [187 189 195 255]
  [114 117 121 255]
  [ 54  57  62 255]]

 [[201 215 224 255]
  [201 215 224 255]
  [201 215 224 255]
  ...
  [164 167 176 255]
  [ 92  95 102 255]
  [ 57  60  66 255]]

 ...

 [[162 156 140 255]
  [162 156 140 255]
  [162 156 140 255]
  ...
  [123 107  79 255]
  [123 106  80 255]
  [140 122  97 255]]

 [[159 153 137 255]
  [158 152 136 255]
  [158 152 136 255]
  ...
  [138 122  94 255]
  [141 124  98 255]
  [130 111  86 255]]

 [[157 151 135 255]
  [157 151 135 255]
  [157 151 135 255]
  ...
  [122 109  75 255]
  [114 101  67 255]
  [100  87  53 255]]]
Converting Image To Arrays
Converting Image To Arrays.

Method 3 – Transform Images with Scikit-image

Using scikit-image is also very easy in order to convert an image into a numpy array. We will use the io.imread() function to read the image and convert it to a numpy array.

#importing required modules
from skimage import io
#storing the image in a variable and converting
imagenotoriginal = io.imread('/content/Sample.png')
#displaying the type of the picture after conversion and other details
print("The type of the image now is=",type(imagenotoriginal))
print("The daatatype of the image is=",imagenotoriginal.dtype)
print("The dimensions of the original image are= ",imagenotoriginal.shape)

The output will be:

The type of the image now is= <class 'numpy.ndarray'>
The daatatype of the image is= uint8
The dimensions of the original image are=  (600, 1200, 4)
Using Skimage
Using Skimage

Method 4 – Image to Numpy Array Conversion via Keras API

Keras is a deep learning API for artificial neural networks for python. It needs tensorflow as it uses its interface to work. This method is also useful for converting images to numpy arrays. Let’s look at its implementation.

#importing required API and modules
import warnings
from tensorflow.keras.utils import img_to_array
from tensorflow.keras.utils import load_img
#loading the image using load_img
org_img= load_img('/content/Sample.png') 
# details about the original image is displayed
print("The mode of the original image= ",org_img.mode)
print("The dimensions of the original image= ",org_img.size) 
# converting the given image into an numpy array
img_array = img_to_array(org_img)
print("Details about the image after conversion:") 
#displaying the type of the image 
print("The datatype of the image now = ",type(img_array))
# displaying the datatype of the elements of array
print("The type of the array:", img_array.dtype)
# displaying dimensions of the image
print("The shape of the image is:", img_array.shape)

The output of the above code would be:

The mode of the original image=  RGB
The dimensions of the original image=  (1200, 600)
Details about the image after conversion:
The datatype of the image now =  <class 'numpy.ndarray'>
The type of the array: float32
The shape of the image is: (600, 1200, 3)
Using Keras API
Using Keras API

Related: Predict Shakespearean Text Using Keras TensorFlow.

Choose the Right Method for Your Needs

Images can be fed into complex neural networks in the form of numpy arrays for easier operations. Deep learning algorithms implicitly convert images into arrays behind the curtain, but it can also be done explicitly by the programs and users according to use.

Python is an extremely adaptable language, hence artificial networks and machine learning models are coded in this language. With high level APIs and various libraries image manipulation becomes a piece of cake in the python environment. To know more about image manipulation with python, click here.

As you venture into more complex image processing and machine learning tasks, consider how the choice of a particular library influences your workflow and results. Are there other libraries or tools that may better serve your needs? How can you optimize your image processing pipeline to enhance the performance of your machine learning models? And most importantly, what new insights can you uncover by harnessing the power of Python and its vast ecosystem of libraries for image manipulation?