Numpy is a Python module that allows you to efficiently do mathematical computation work. Suppose you have an image and want to convert it to NumPy array for further computation then how you can do so? In this tutorial, you will know how to convert image to numpy array in Python using various methods.

## Methods to Convert Image to NumPy Array in Python

In this section, you will know all the methods that you will implement to convert the image to NumPy array. Let’s get started. Below is the image I am using in all the methods.

### Method 1: Convert the image to NumPy array using the pillow library

The pillow module allows you to open, manipulate and save images in different formats. This module has a simple interface for resizing, cropping, rotating, filtering, and adding text to images.

In this method, you will import the Image module from the pillow package. After that, you will open your image using the Image.open() function. Lastly to convert the image to numpy array you will use the numpy.array() function.

Run the below lines of code to convert the image to NumPy array.

```
from PIL import Image
import numpy as np
# Load image
img = Image.open('ai-image.jpg')
# Convert image to NumPy array
img_array = np.array(img)
print(img_array)
```

**Output**

## Method 2: Using the opencv package

The other method to convert the image to a NumPy array is the use of the OpenCV library. Here you will use the cv2.imread() function to read the input image and after that convert the image to NumPy array using the same numpy.array() function.

Execute the below lines of code to achieve the conversion.

```
import cv2
import numpy as np
# Load image using OpenCV
img = cv2.imread('ai-image.jpg')
# Convert image to NumPy array
img_array = np.array(img)
print(img_array)
```

**Output**

### Method 3: Use the imageio library

```
import imageio
import numpy as np
# Load image using imageio
img = imageio.imread('ai-image.jpg')
# Convert image to NumPy array
img_array = np.array(img)
print(img_array)
```

**Output**

The third method is the use of imageio library. The imagio library allows you to read and save images in multiple file formats including PNG, JPEG, GIF, BMP, TIFF, WebP, and many others. It can also handle video file formats. To convert the image to numpy array using it you have to first read the image file using the imagio.read() function. After that you will use the numpy.array() function.

Use the below lines of code to do the image conversion to the NumPy array.

## Conclusion

There are many different tasks you can do after you convert the image to a NumPy array. The above methods allow you to do so. You can resize the image, crop the image, and rotate the image using the numpy array.

I hope you have liked this tutorial. You can use any method you like as all allow you to convert the image to NumPy array. If you have any doubts or queries then you can contact us for more help.

#### Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.