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

  • Count files in folder in PowerShell
    01 January

    PowerShell – Count Files in Folders

    Table of ContentsCount Files in Folders in PowerShellCount Files in multiple Folders in PowerShellCount Files in Folders and Subfolders in PowerShellCount only folders inside folder in PowerShellCount Files by Extension in Folder in PowerShell Count Files in Folders in PowerShell We can use the Measure-Object cmdlet in different ways to meet our project requirements. However, […]

  • Check if Object contains property in PowerShell
    31 December

    Check if Object has Property in PowerShell

    Table of ContentsUsing the -match ParameterUsing -contains ParameterUsing if-else BlockUsing .Match() Function with if-else BlockUsing Get-Member Cmdlet Using the -match Parameter Use the -match parameter to check if an object has a property in PowerShell. [crayon-63b35d6cb4c7b653220900/] [crayon-63b35d6cb4c80600446099/] Here, we used the -match parameter to check if the given object, which is $result in our case, […]

  • Create Empty File in PowerShell
    28 December

    Create Empty File in PowerShell

    Table of ContentsUsing New-Item CmdletUse the ni AliasUsing Out-File CmdletUsing the fsutil file CommandUsing Text EditorUsing echo CommandUsing for Loop Using New-Item Cmdlet Use the New-Item cmdlet to create an empty .txt file in PowerShell. [crayon-63b35d6cb4e29523377864/] [crayon-63b35d6cb4e2b450130690/] We used the New-Item cmdlet with -Path, -Name, and -ItemType parameters to create an empty .txt file using […]

  • 27 December

    Run PowerShell as Another User

    Table of ContentsUsing the runas CommandOpen the PowerShell as an AdministratorRun the runas commandUsing the Start-Process cmdlet with the Credential ParameterUI-Based SolutionScript-Based Solution Using the runas Command The runas command in PowerShell launches programs using credentials different from the current user. It provides users with limited privileges to execute commands or access resources available only […]

  • 27 December

    Create Array of All NaN Values in Python

    Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63b35d6cb5110335682595/] [crayon-63b35d6cb5113561644155/] We used numpy.empty() […]

  • 27 December

    Call Function from Another Function in Python

    Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63b35d6cb5293732719758/] [crayon-63b35d6cb5296541112262/] To call a function, […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.