我有一个Numpy数组类型的矩阵。我怎么把它作为映像写到磁盘上呢?任何格式都可以(png, jpeg, bmp…)一个重要的限制是PIL不存在。
当前回答
你可以在Python中使用'skimage'库
例子:
from skimage.io import imsave
imsave('Path_to_your_folder/File_name.jpg',your_array)
其他回答
对于那些寻找一个直接的充分工作的例子:
from PIL import Image
import numpy
w,h = 200,100
img = numpy.zeros((h,w,3),dtype=numpy.uint8) # has to be unsigned bytes
img[:] = (0,0,255) # fill blue
x,y = 40,20
img[y:y+30, x:x+50] = (255,0,0) # 50x30 red box
Image.fromarray(img).convert("RGB").save("art.png") # don't need to convert
此外,如果你想要高质量的jpeg .save(file, subsampling=0, quality=100)
你可以在Python中使用'skimage'库
例子:
from skimage.io import imsave
imsave('Path_to_your_folder/File_name.jpg',your_array)
如果你有matplotlib,你可以这样做:
import matplotlib.pyplot as plt
plt.imshow(matrix) #Needs to be in row,col order
plt.savefig(filename)
这将保存情节(而不是图像本身)。
你可以使用这段代码将你的Npy数据转换成图像:
from PIL import Image
import numpy as np
data = np.load('/kaggle/input/objects-dataset/nmbu.npy')
im = Image.fromarray(data, 'RGB')
im.save("your_file.jpeg")
如果你在python环境Spyder中工作,那么它不能比在变量资源管理器中右键单击数组更容易,然后选择显示图像选项。
这将要求您将图像保存到dsik,主要是PNG格式。
在这种情况下不需要PIL库。