python - 将python中的numpy.ndarray保存为图像

标签 python

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

Output of ndarray 希望这对您有所帮助!

关于python - 将python中的numpy.ndarray保存为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13811334/

相关文章:

python - 如何编写 pep8 配置(pep8.rc)文件?

python - .obj 文件中的面是什么以及它们如何工作?

javascript - 投票功能 "No Eintrag matches the given query"

python - 如何使用附加信息重新引发异常?

python - Pycharm不显示输出

Python - 在先前已在全局范围内查找的函数内重新分配名称

python - 同时将多列的类型从日期时间更改为日期(pandas)

python - 如何计算数组 block 内值的总和

Python 函数属性 - 使用和滥用

Python 检查文件是否存在,如果存在则更新它,否则创建它