Save Numpy Array as Image in Python

In this post, we will see how to save numpy array as image in Python.

Save numpy array as image in Python

Python provides a wide range of libraries that can be utilized to read and process images efficiently. Images are also processed efficiently while carrying out Computer Vision tasks. But how does Python store these images?

These images are stored and processed in the form of a numpy array. Every image stores a pixel in a specific color code at a given position. They are stored as numpy arrays and represent the image as [Height, Width, Channel] in a 3-D array.

How to save numpy array as image in Python?

We will now discuss different methods to save numpy array as image in Python.

Using the PIL library to save numpy array as image in Python

The Python Imaging Library was one of the standard packages for image processing. Support for this package stopped in 2011, and after that, a fork of the same package as PIL was added as a standard library.

We can use functions from this library to save numpy array as image in Python.

First, we will define an array that represents an image. Then, we will use the fromarray() function to read this array as an image object. This object will be exported to an external file using the save() function.

See the code below.

In the above example, we create an array and export it as an image. The filename and path are specified within the save() function.
Here is image created from above numpy array.
Save numpy array as image

All the other methods will also generate same image.

Using the matplotlib.image library to save numpy array as image in Python

The matplotlib library is one of the standard libraries that is used for creating graphical plots. It is based on the matlab interface. The matplotlib.image class of this library allows us to read and process images.

To save numpy array as image in Python, we can use the imsave() method from this library. In this function, we can specify the array we want to export as an image and the path with the filename for the image.

See the code below.

Using the opencv library to save numpy array as image in Python

The opencv library is an open-source library available in multiple programming languages. It is available in Python and has different algorithms available to help in Computer Vision tasks. We can process images very efficiently with this library.

The imwrite() function from this library can be used to save numpy array as image in Python. It can export a given array as an image file. Works similar to the imsave() function of the previous method.

It returns True if the operation is done successfully.

For example,

Output:

True

Using the imageio library to save numpy array as image in Python

The imagio library is also a cross-platform library that is used to process images. It is available in Python 3.5 and above. The functionalities are fast and easy to use.

The imwrite() function can be used to save numpy array as image in Python, similar to the previous method.

For example,

Using the scipy.misc library to save numpy array as image in Python

The imageio is a relatively new addition to Python and before 2018 it existed in the scipy.misc module. So, for older versions, we can use the imsave() function from the scipy.misc module to save numpy array as image in Python.

For example,

Conclusion

To conclude, we discussed how to save numpy array as image in this article. We can use different libraries for this.

The most used method is by using the PIL library, which is the standard library for image processing. The matplotlib.image module can also be used for the same.

The opencv library has many functionalities for Computer Vision tasks and can save numpy array as image in Python using the imwrite() function. The final two methods use the imageio library and the scipy.misc module.

The imageio is a recent introduction in Python and earlier used to exist in the scipy.misc module. The scipy.misc module is now deprecated.

That’s all about how to save numpy array as image in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *