python - 如何保存图片数组以及与之相关的信息?
问题描述
我是刮车的,会拍很多图,这部分没问题。我也想保存汽车规格。我想知道有效地做到这一点的最佳方法。理想情况下,我会在许多库中拥有类似内置数据集的东西。如:
print(dataset)
{
'图像': ([255, 203, 145, ...]),
'信息': (['奥迪', '355 HP', ...])
}
这样,我可以很容易地用dataset['info']
, 或其他东西提取图像和信息。我可以很容易地分配两个 like x, y = dataset
。
解决方案
有多种选择,但对于像这样的结构化数据,通常使用 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()
推荐阅读
- bootstrap-tour - Bootstrap 导览背景在步骤之间闪烁
- java - 用于将 SELECT 的结果插入 Spring Boot JPA Hibernate 中的表的自定义 SQL 查询
- delphi - 为什么在Delphi中BitMap的传输顺序相反?
- powerbi - 如何查找由不同列排序的最后一个值
- activejdbc - ActiveWeb - 渲染父属性值
- angular - NGINX HTTP2 推送 - Angular
- linux - 带有访问令牌的 git pull,带有用户名/密码的 git push
- angular - Why my file app.component.html is not working?
- r - 使用 rbind 将数据作为列而不是 r 中的行添加到数据帧
- php - Symfony 3.4 服务 - Doctrine\ORM\EntityManager 的实例,给定的布尔值