The values of each pixel in the NumPy array image can be obtained by printing the data returned by the asarray()
or array()
functions.
Key takeaways:
Converting a PIL image to NumPy array helps in image processing and numerical analysis.
Use
pip install Pillow numpy
to get Pillow for image handling and NumPy for numerical operations.Use
Image.open('filename')
from Pillow to load your image into a PIL.Use
np.array(image)
ornp.asarray(image)
to convert the PIL image to a NumPy array for numerical manipulation.
When working with Python, we may encounter scenarios where we have to process images and perform tasks. The Python Imaging Library (PIL) by Fredrik Lundh is used when working with images. On the other hand, NumPy helps in numerical computation and supports n-dimensional arrays.
In image processing tasks, we usually convert the images into NumPy arrays because this allows for faster and more efficient operations on large arrays and datasets. In this Answer, we’ll learn how to convert a PIL image to a NumPy array.
To get started, we need to install PIL and NumPy in our system. For that, we can use the following command:
pip install Pillow numpy
Note: Pillow is the newer version of PIL.
Once the libraries are installed successfully, we can move to the next steps.
We’ll use the following image and convert it into a NumPy array.
Now that we have installed the PIL library, we can load the image into our program. For that, we can use the following code:
from PIL import Image img = Image.open("bottle.jpeg") print(img)
Line 1: We import Image
module from the PIL
library.
Line 3: We open the image using the Image.open()
function and pass the file name of the image as a parameter.
Line 5: We print the image, which shows that the image is a JPEG image.
np.array()
to convert PIL images to NumPy arrayTill now, we have installed the required libraries and loaded the image in our program. Now, we’ll convert the image to a NumPy array using the following code:
from PIL import Image import numpy as np img = Image.open("bottle.jpeg") numpy_array = np.array(img) print(numpy_array.shape)
Line 2: We import numpy
library as np
.
Line 5: We convert the image loaded in the img
variable to a NumPy array using the np.array()
function.
Line 6: We print out the shape
of the NumPy array.
np.asarray()
to convert PIL images to NumPy arrayIn the code above, we can also use the np.asarray()
function instead of the np.array()
function. The main difference between both of the functions is that using np.array()
, a copy of the original array is created, and the changes made to the original array are not reflected in the copy array. On the other hand, np.asarray()
reflects all changes made to the original array in place. However, since the input is an image here and not an array, both methods will act the same way.
from PIL import Image import numpy as np img = Image.open("bottle.jpeg") numpy_array = np.asarray(img) print(numpy_array.shape)
The NumPy array is dimensioned according to the image’s height, width, and color channels. In our case, the NumPy array’s dimension is (1600, 1200, 3)
, which corresponds to (the height, width, and channels) of the image.
Let’s quickly assess your understanding of converting a PIL image to a NumPy array by the following quiz:
What is the primary function used to open an image file with Pillow?
Image.load()
Image.read()
Image.open()
None of them
Once the image has been converted to a NumPy array, we can leverage the power of the NumPy functions to perform various image processing tasks, such as filtering, blurring, and rotating an image.
Haven’t found what you were looking for? Contact Us
Free Resources