[英]Saving numpy.ndarray in python as an image
b=ndimage.gaussian_filter(imagefile,5)
是python的新手,无法弄清楚这一点。
如何将b
保存为图像, b
的类型为'numpy.ndarray'?
试了这些
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.
将ndarray
保存到图像中的正确方法是ndarray
?
编辑:
解决了:
im = Image.fromarray(b)
im.save('newfile.jpeg')
起作用了,我加载图像的方式是错误的,
file = Image.open("abc.jpg")
imagefile = file.load()
//加载后我正在使用imagefile,它没有提供适当的形状来重建图像。
//相反,如果我使用文件(即直接在打开后,可以通过上述方法保存)
我认为最好的方法是使用matplotlib
imshow
。
使用image
库:
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版本:
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")
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.