Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a Numpy array as an image

I have a matrix in the type of a Numpy array. How would I write it to disk it as an image? Any format works (png, jpeg, bmp...). One important constraint is that PIL is not present.

like image 691
M456 Avatar asked May 24 '09 00:05

M456


People also ask

Can I save a NumPy array?

You can save your NumPy arrays to CSV files using the savetxt() function. This function takes a filename and array as arguments and saves the array into CSV format. You must also specify the delimiter; this is the character used to separate each variable in the file, most commonly a comma.


1 Answers

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

given a numpy array "A":

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

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

like image 198
migas Avatar answered Sep 20 '22 18:09

migas