Save NumPy Array as Image in Python

Manav Narula Jan 30, 2023 Apr 19, 2021 NumPy NumPy Image
  1. Use the Image.fromarray() Function to Save a NumPy Array as an Image
  2. Use the imageio.imwrite() Function to Save a NumPy Array as an Image
  3. Use the matplotlib.pyplot.imsave() Function to Save a NumPy Array as an Image
  4. Use the cv2.imwrite() Function to Save a NumPy Array as an Image
Save NumPy Array as Image in Python

In Python, the numpy module is used to work with arrays. There are many modules available in Python that allow us to read and store images.

Images can be thought of as an array of different pixels stored at specific positions with respective color codes. So, we might encounter situations where we need to convert and save an array as an image.

In this tutorial, we will discuss how to save a numpy array as an image.

Use the Image.fromarray() Function to Save a NumPy Array as an Image

The fromarray() function is used to create an image memory from an object which exports the array. We can then save this image memory to our desired location by providing the required path and the file name.

For example,

import numpy as np
from PIL import Image

array = np.arange(0, 737280, 1, np.uint8)
array = np.reshape(array, (1024, 720))

im = Image.fromarray(array)
im.save("filename.jpeg")

We first create an array that stores RGB color codes and then export them. We can specify the desired format of our image in the file name. This can be jpeg, png, or any other commonly used image format. This is common for all the methods discussed below.

Use the imageio.imwrite() Function to Save a NumPy Array as an Image

Earlier, the scipy module had the imsave() function to save numpy arrays as images. However, in recent versions, it got deprecated and the imwrite() function from the image.io() started getting recommended to perform this task and gained popularity for its simplicity.

The following code shows how to use this function.

import imageio
import numpy as np

array = np.arange(0, 737280, 1, np.uint8)
array = np.reshape(array, (1024, 720))

imageio.imwrite('filename.jpeg', array)

Use the matplotlib.pyplot.imsave() Function to Save a NumPy Array as an Image

The matplotlib module has a variety of functions available for working with images.

The imsave() function can save an array as an image file.

For example,

import matplotlib.pyplot as plt
import numpy as np

array = np.arange(0, 737280, 1, np.uint8)
array = np.reshape(array, (1024, 720))

plt.imsave('filename.jpeg', array)

Use the cv2.imwrite() Function to Save a NumPy Array as an Image

The OpenCV module is ofen used for image processing in Python. The imwrite() function from this module can export a numpy array as an image file.

For example,

import cv2   
import numpy as np

array = np.arange(0, 737280, 1, np.uint8)
array = np.reshape(array, (1024, 720))

cv2.imwrite('filename.jpeg', array)
Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn