在python中将numpy.ndarray保存为imag

2024-06-28 19:57:58 发布

您现在位置:Python中文网/ 问答频道 /正文

b=ndimage.gaussian_filter(imagefile,5)   

对python还不熟悉,无法理解这一点。
如何将b保存为图像,b属于“numpy.ndarray”类型?

试过这些,
一。

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保存到图像中的正确方法是什么?

编辑:

已解决:

im = Image.fromarray(b)    

im.save('newfile.jpeg')起作用了,我加载图像的方式是错误的

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

//I was using imagefile after loading,which was not giving proper shape to reconstruct the image.加载后,我正在使用imagefile,它没有给出重建图像的正确形状。

//如果我使用文件(即打开后直接使用上述方法保存)


Tags: 方法图像imagesavenoterrorfilejpeg
1条回答
网友
1楼 · 发布于 2024-06-28 19:57:58

我认为最好的方法是使用matplotlibimshow

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

Output of ndarray 希望这有帮助!

相关问题 更多 >