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

0

1 Answer 1

6

I think the best approach is using matplotlib imshow.

using the image library:

import Image
import numpy as np

x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)

im = Image.fromarray(x)
im.save('test.png')

Matplotlib Version:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)
plt.imshow(x) 
plt.savefig("array")

Output of ndarray Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Let me try, I would like to avoid importing few more libraries to just save an array as image.
You can add your previous answer of saving the image, I can save it that way, provided I read the image properly, I have edited the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.