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?


import_contacts

You may also like:

Related Posts

  • 29 June

    Get Day Name from Date in Python

    Table of ContentsUse the Calendar Module to Get the Name of the Day in PythonUse the strftime() Method to Get the Name of the Day in PythonUse Pandas Timestamp Method to Get the Name of the Day in PythonUse the weekday() Method to Get the Name of the Day in PythonUse the isoweekday() Method to […]

  • 27 June

    Fill Array with Random Numbers in Python

    Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusionWas this post helpful? Fill array […]

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusionWas this post helpful? Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.