Saving a Numpy array as an image Saving a Numpy array as an image python python

Saving a Numpy array as an image

An answer using PIL (just in case it's useful).

given a numpy array "A":

from PIL import Imageim = Image.fromarray(A)"your_file.jpeg")

you can replace "jpeg" with almost any format you want. More details about the formats here

This uses PIL, but maybe some might find it useful:

import scipy.miscscipy.misc.imsave('outfile.jpg', image_array)

EDIT: The current scipy version started to normalize all images so that min(data) become black and max(data) become white. This is unwanted if the data should be exact grey levels or exact RGB channels. The solution:

import scipy.miscscipy.misc.toimage(image_array, cmin=0.0, cmax=...).save('outfile.jpg')

With matplotlib:

import matplotlibmatplotlib.image.imsave('name.png', array)

Works with matplotlib 1.3.1, I don't know about lower version. From the docstring:

Arguments:  *fname*:    A string containing a path to a filename, or a Python file-like object.    If *format* is *None* and *fname* is a string, the output    format is deduced from the extension of the filename.  *arr*:    An MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA) array.

enter image description here