Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving numpy.ndarray in python as an image

Tags:

b=ndimage.gaussian_filter(imagefile,5)   

Being new to python, not able to figure this out.
how to save b as an image, b is of type 'numpy.ndarray'?

Tried these,
1.

im = Image.fromarray(b)   
im.save("newfile.jpeg")   

Error: TypeError("Cannot handle this data type")

2.

imsave('newfile.jpg', b)

Error: ValueError: 'arr' does not have a suitable array shape for any mode.

Which is the right way to save an ndarray into an image?

EDIT:

Solved:

im = Image.fromarray(b)    

im.save('newfile.jpeg') worked, The way I was loading the image was wrong,

file = Image.open("abc.jpg")      
imagefile = file.load()     

// I was using imagefile after loading, which was not giving proper shape to reconstruct the image.

// Instead If I use file (i.e. directly after opening, I can save by above method)