Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a 3 channel numpy array as image

I have a numpy array with shape (3, 256, 256) which is a 3 channel (RGB) image of resoulution 256x256. I am trying to save this to disk with Image from PIL by doing the following:

from PIL import Image
import numpy as np

#... get array s.t. arr.shape = (3,256, 256)
img = Image.fromarray(arr, 'RGB')
img.save('out.png')

However this is saving an image of dimensions 256x3 to disk

like image 717
Aly Avatar asked Dec 22 '14 10:12

Aly


People also ask

How do I save an array image in NumPy?

To save a Numpy array as an image with Python, we can use the Image. fromarray method. We call numpy. zeroes to generate an array and assign that to img .

How do you create a 3-dimensional NumPy array?

A three dimensional means we can use nested levels of array for each dimension. To create a 3-dimensional numpy array we can use simple numpy. array() function to display the 3-d array.

How do I save a NumPy array as an image?

If you want to save a numpy array as an image, use the imageio.imwrite () Function. You can save a numpy array as an image using the matplotlib.pyplot.imsave () Function. If you want to save a numpy array as an image, use the cv2.imwrite () function. How Do I Print An Image Array In Python? from PIL import Image. import numpy as np.

How to convert an array to image in Python?

NumPy can be used to convert an array into image. Apart from NumPy we will be using PIL or Python Image Library also known as Pillow to manipulate and save arrays. Create a numpy array. Reshape the above array to suitable dimensions. Create an image object from the above array using PIL library.

How to get the value of each pixel in a NumPy array?

This function converts the input to an array By using numpy.array () function which takes an image as the argument and converts to NumPy array In order to get the value of each pixel of the NumPy array image, we need to print the retrieved data that got either from asarray () function or array () function.

How to get the image from converted NumPy array in keras?

Image.fromarray () function helps to get back the image from converted numpy array. We get back the pixels also same after converting back and forth. Hence, this is very much efficient Keras API provides the functions for loading, converting, and saving image data.


2 Answers

The @Dietrich answer is valid, however in some cases it will flip the image. Since the transpose operator reverses the index, if the image is stored in RGB x rows x cols the transpose operator will yield cols x rows x RGB (which is the rotated image and not the desired result).

>>> arr = np.random.uniform(size=(3,256,257))*255

Note the 257 for visualization purposes.

>>> arr.T.shape
(257, 256, 3)

>>> arr.transpose(1, 2, 0).shape
(256, 257, 3)

The last one is what you might want in some cases, since it reorders the image (rows x cols x RGB in the example) instead of fully transpose it.

>>> arr = np.random.uniform(size=(3,256,256))*255
>>> arr = np.ascontiguousarray(arr.transpose(1,2,0))
>>> img = Image.fromarray(arr, 'RGB')
>>> img.save('out.png')

Probably the cast to contiguous array is not even needed, but is better to be sure that the image is contiguous before saving it.

like image 106
Imanol Luengo Avatar answered Oct 19 '22 15:10

Imanol Luengo


Try transposing arr which gives you an (256, 256, 3) array:

arr = np.random.uniform(size=(3,256,256))*255
img = Image.fromarray(arr.T, 'RGB')
img.save('out.png')
like image 42
Dietrich Avatar answered Oct 19 '22 13:10

Dietrich