saving numpy.ndarray in python as an image

  • Last Update :
  • Techknowledgy :

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")

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)
3._
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)

Suggestion : 2

Last Updated : 02 Sep, 2020

Output:

<class 'numpy.ndarray'>
   (737280,)
   (1024, 720)
   [[ 0 1 2 ... 205 206 207]
   [208 209 210 ... 157 158 159]
   [160 161 162 ... 109 110 111]
   ...
   [144 145 146 ... 93 94 95]
   [ 96 97 98 ... 45 46 47]
   [ 48 49 50 ... 253 254 255]]

Suggestion : 3

Post date October 24, 2021

For instance, we write:

from PIL
import Image
import numpy

w, h = 200, 100
img = numpy.zeros((h, w, 3), dtype = numpy.uint8)

img[: ] = (0, 0, 255)

x, y = 40, 20
img[y: y + 30, x: x + 50] = (255, 0, 0)

Image.fromarray(img).convert("RGB").save("art.png")

We then change the colors of some of the entries in the img to (255, 0, 0) with:

x, y = 40, 20
img[y: y + 30, x: x + 50] = (255, 0, 0)

Suggestion : 4

To save the Numpy array as a local image, use the save() function and pass the image filename with the directory where to save it.,With this article at OpenGenus, you must have the complete idea of how to display and save Numpy array as image.,In this article, we have explained the process of how to display and save a Numpy array as an Image. This requires Numpy and PIL library.,Create a sample Numpy array and convert the array to PIL format using fromarray() function of PIL.

Import the relevant library to display and save Numpy array as Image. We import numpy library to create Numpy array and PIL library to add support for display and saving.

from PIL
import Image
import numpy as np

Create a sample Numpy array and convert the array to PIL format using fromarray() function of PIL.

height = 500
weight = 500
channel = 3
img_numpy = np.zeros((height, weight, channel), dtype = np.uint8)
img = Image.fromarray(img_numpy, "RGB")

To view the image from the terminal use the show() function:

# Display the Numpy array as Image
img.show()

Following is the complete Python code to display and save Numpy array as Image:

from PIL
import Image
import numpy as np

height = 500
weight = 500
channel = 3
img_numpy = np.zeros((height, weight, channel), dtype = np.uint8)
img = Image.fromarray(img_numpy, "RGB")
# Display the Numpy array as Image
img.show()

# Save the Numpy array as Image
image_filename = "opengenus_image.jpeg"
img.save(image_filename)

Suggestion : 5

For a description of the .npy format, see numpy.lib.format.,Save an array to a binary file in NumPy .npy format.,Any data saved to the file is appended to the end of the file.,File or filename to which the data is saved. If file is a file-object, then the filename is unchanged. If file is a string or Path, a .npy extension will be appended to the filename if it does not already have one.

>>> from tempfile
import TemporaryFile
   >>>
   outfile = TemporaryFile()
>>> x = np.arange(10) >>>
   np.save(outfile, x)
>>> _ = outfile.seek(0) # Only needed here to simulate closing & reopening file >>>
   np.load(outfile)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> with open('test.npy', 'wb') as f:
   ...np.save(f, np.array([1, 2]))
   ...np.save(f, np.array([1, 3])) >>>
   with open('test.npy', 'rb') as f:
   ...a = np.load(f)
   ...b = np.load(f) >>>
   print(a, b)
#[1 2][1 3]