![]() |
VOOZH | about |
Pictures on a computer are made of tiny dots called pixels. To work with them in Python, we convert them into numbers using a NumPy array is a table of numbers showing each pixel’s color. In this article, we’ll learn how to do this using popular Python tools.
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:
Output
PNG
(400, 200)
RGB
Explanation: It imports the Image class and loads 'Sample.png' using Image.open(). It then retrieves the image’s format (e.g., PNG), size (width, height) and color mode (e.g., RGB or L).
While Pillow is great for opening and viewing images, NumPy enables powerful numerical operations. By converting images to arrays using np.array() or np.asarray(), you can represent them as matrices (or 3D tensors for RGB images).
Example 1: In this example, we use asarray() from NumPy to convert an image into a NumPy array.
Output
<class 'numpy.ndarray'> (200, 400, 3)
Explanation: It loads 'Sample.png' using Image.open() and converts it to a NumPy array with asarray(), enabling numerical operations. The result is a 3D array of shape (200, 400, 3) representing a color image.
Example 2: In this example, we use np.array() from NumPy to convert an image into a NumPy array.
Output
(200, 400, 3)
Explanation: It loads 'Sample.png' using Image.open() and converts it to a NumPy array using np.array(). The resulting array has a shape of (200, 400, 3), indicating a color image with height 200, width 400 and 3 color channels (RGB).
Example 3: Here we display the actual pixel data from the image in NumPy array format.
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]]
...
]]
Explanation: It loads 'Sample.png' and converts it to a NumPy array using asarray(), then prints the raw RGB pixel values row by row, showing the image's numerical structure.
Example 4: In this we use Image.fromarray() to revert back from NumPy array to image format.
Output
<class 'numpy.ndarray'>
(200, 400, 3)
<class 'PIL.Image.Image'>
RGB (400, 200)
Explanation: It loads 'Sample.png', converts it to a NumPy array with asarray(), then back to a PIL image using Image.fromarray(), enabling easy switching between array and image formats for processing and restoration.
If you're working with deep learning in TensorFlow or Keras, Keras image preprocessing utilities streamline image handling:
These functions support resizing and normalization, making them ideal for preparing data for Convolutional Neural Networks (CNNs) during training and inference.
Example 1: In this example, we use the load_img() function from the Keras API to load an image file in PNG format.
Output
<class 'PIL.PngImagePlugin.PngImageFile'>
PNG
RGB
(400, 200)
Explanation: It uses Keras’s load_img() to load 'Sample.png' and returns it as a PIL image. The code then prints the image’s type, format (PNG), color mode (RGB) and dimensions (width, height).
Example 2: In this example, we use the Keras API functions img_to_array() and array_to_img() to convert a PIL image to a NumPy array and then convert it back to an image.
Output
<class 'numpy.ndarray'>
type: float32
shape: (200, 400, 3)
converting NumPy array into image: <class 'PIL.Image.Image'>
Explanation: It converts a PIL image to a NumPy array using img_to_array() for processing, then back to a PIL image using array_to_img(), confirming smooth transformation with array type float32 and shape (200, 400, 3).
For advanced, high-performance image processing, OpenCV is a powerful open-source library that operates directly on images as NumPy arrays, avoiding extra conversions. Unlike PIL and Keras, it loads images in BGR format, so converting to RGB with cv2.cvtColor() is essential for tasks like classification or segmentation.
Output
<class 'numpy.ndarray'>
Explanation: Loads 'Sample.png' using OpenCV’s cv2.imread() (BGR format), converts it to RGB with cv2.cvtColor() and saves it using cv2.imwrite(). Confirms OpenCV treats images as NumPy arrays by default.