首页 > 解决方案 > 如何保存图片数组以及与之相关的信息?

问题描述

我是刮车的,会拍很多图,这部分没问题。我也想保存汽车规格。我想知道有效地做到这一点的最佳方法。理想情况下,我会在许多库中拥有类似内置数据集的东西。如:

print(dataset)

{

'图像': ([255, 203, 145, ...]),

'信息': (['奥迪', '355 HP', ...])

}

这样,我可以很容易地用dataset['info'], 或其他东西提取图像和信息。我可以很容易地分配两个 like x, y = dataset

标签: pythonimagenumpy

解决方案


有多种选择,但对于像这样的结构化数据,通常使用 hdf5 存储字典。

在此处查看 python 教程和完整文档

http://docs.h5py.org/en/stable/quick.html


这是一个完整的python示例。注意字典一样的界面。

import h5py
import numpy as np

#####
#writing output file
#####
my_file = h5py.File("output.h5",'w')
my_file['info']  = np.string_("some_random pixels")    #hdf5 needs numpy to store strings
my_file['image'] = np.random.rand(5,5)
my_file.close()
#####
#reading input file
#####
loaded_file = h5py.File("output.h5",'r')
print(np.array(loaded_file['info']))                  #hdf5 also needs numpy to read strings as well
print(np.array(loaded_file['image']))
loaded_file.close()

推荐阅读