b=ndimage.gaussian_filter(imagefile,5)
初学python,不太懂。
如何将 b
保存为图像,b
是 'numpy.ndarray' 类型?
试过这些,
1.
im = Image.fromarray(b)
im.save("newfile.jpeg")
错误:TypeError("无法处理此数据类型")
2.
imsave('newfile.jpg', b)
错误:ValueError:“arr”没有适合任何模式的数组形状。
将 ndarray
保存到图像中的正确方法是什么?
编辑:
已解决:
im = Image.fromarray(b)
im.save('newfile.jpeg')
有效,我加载图像的方式是错误的,
file = Image.open("abc.jpg")
imagefile = file.load()
//我在加载后使用图像文件,它没有提供正确的形状来重建图像。
//相反,如果我使用文件(即打开后直接,我可以通过上述方法保存)
最佳答案
我认为最好的方法是使用 matplotlib
imshow
。
使用图像
库:
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")
希望这对您有所帮助!
关于python - 将python中的numpy.ndarray保存为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13811334/