How to Convert an Image to a NumPy Array and Save it to a CSV file using Python?


Python is a powerful programming language with a vast array of libraries and modules. One such library is NumPy, which is used for numerical computing and processing large multidimensional arrays and matrices. Another popular library used for image processing in Python is Pillow, which is a fork of the Python Imaging Library (PIL).

In this tutorial, we will show you how to convert an image to a NumPy array and save it to a CSV file using Python. We will be using the Pillow library to open the image and convert it to a NumPy array, and the CSV module to save the NumPy array to a CSV file. In the next section of the article, we will cover the steps required to convert an image to a NumPy array using the Pillow library. So, let's dive in!

How to Convert an Image to a NumPy Array and save it to a CSV file using Python?

Before we dive into the process of converting an image to a NumPy array and saving it to a CSV file, let's first understand the two libraries we'll be using in this tutorial: Pillow and NumPy.

Pillow is a Python Imaging Library (PIL) that adds support for opening, manipulating, and saving many different image file formats.

NumPy is a fundamental library for scientific computing in Python. It provides support for large multidimensional arrays and matrices, along with a range of mathematical functions to operate on them.

To use these libraries, we first need to install them on our system. We can do this using pip, the Python package installer.

Here's how to install Pillow:

pip install Pillow

And here's how to install NumPy:

pip install numpy

Now that we have installed the necessary libraries, let's move on to the next section of the article to convert an image to a NumPy array.

Convert Image to NumPy array

Consider the below code for converting an image to a Numpy array:

# Import necessary libraries
import csv
from PIL import Image
import numpy as np

# Open image using Pillow library
img = Image.open('image.jpg')

# Convert image to NumPy array
np_array = np.array(img)

# Save NumPy array to CSV file
np.savetxt('output.csv', np_array, delimiter=',', fmt='%d')

# Print the shape of the NumPy array
print("Shape of NumPy array:", np_array.shape)

In the above code, we first import the necessary libraries csv, PIL and numpy. The CSV library is used to read and write CSV files, while the PIL library is used to open and manipulate images. The NumPy library is used to convert the image to a NumPy array.

Then we open an image file named image.jpg using the Image.open() method from the PIL library. The method returns an Image object.

After that, the image object has been converted to a NumPy array using the np.array() method from the NumPy library. The resulting array contains the pixel values of the image. Finally, we save the NumPy array to a CSV file named output.csv using the np.savetxt() method from the NumPy library. We specify the delimiter as ‘,’ and the format as %d to ensure that the values in the CSV file are separated by commas and are integers.

Lastly, we printed the shape of the NumPy array using the shape attribute. The shape of the NumPy array represents the dimensions of the array, which in this case are height, width, and number of color channels (if applicable).

The output of the above code will create a new file named output.csv in the same directory as the script, containing the pixel values of the image in CSV format and the terminal will display something like this:

Shape of NumPy array: (505, 600, 3)

Here, the shape of the NumPy array is (505, 600, 3), which means that the image has a height and width of 100 pixels each and three color channels (RGB) per pixel.

It is important to note that the shape of the NumPy array depends on the dimensions of the input image. The shape of the array will be (height, width, number of color channels) if the image is a color image and (height, width) if the image is grayscale.

Conclusion

In this article, we have learned how to convert an image to a NumPy array and save it to a CSV file using Python. We used the Pillow library to open and convert the image to a NumPy array and the CSV module to save the NumPy array to a CSV file. We have also covered the steps required to install the necessary libraries and provided an example code for each of the methods. It is important to note that the shape of the NumPy array depends on the dimensions of the input image, and the shape of the array will be different for color and grayscale images. By using this technique, we can easily manipulate and process images using the powerful NumPy library.

Updated on: 21-Jul-2023

403 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements