仅供学习记录分享
import h5py
import cv2
a = r'D:\project\my_script\data\test.hdf5'
img_path = r'D:\project\my_script\data\test.png' # 已有文件
with h5py.File(a, 'w') as db: # 写入
img = cv2.imread(img_path)
print(img.shape) # shape (1024, 1920, 3)
db.create_dataset('curr', data=[img]) # 不放入列表是这种类型的数据
# <HDF5 dataset "curr": shape (1024, 1920, 3), type "|u1">
with h5py.File(a, 'r') as db: # 只读
print(db.keys()) # <KeysViewHDF5 ['curr']>, 我存放了一些图片
print(db['curr'][0].shape)
cv2.imshow('curr', db['curr'][0])
cv2.waitKey()
cv2.destroyAllWindows()
with h5py.File(a, 'a') as db: # 读加写
img = cv2.imread(img_path)
db.create_dataset('next', data=[img])
with h5py.File(a, 'a') as db: # 读加写,
# 若为‘r'会报错KeyError: "Couldn't delete link (no write intent on file)"
print(db.keys())
del db['curr']
print(db.keys())
cv2.imshow('curr', db['next'][0])
cv2.waitKey()
cv2.destroyAllWindows()
随手找的一张图,仅供演示,侵权联系删除