将目录下的图片写入h5py文件

import h5py
from PIL import Image
import matplotlib.image
import matplotlib.pyplot as plt
path="F:\\pythonprogram\\multi_task\\img\\S5\\\\Directions 1.54138969"

##读取文件夹下的图片到列表中
files =os.listdir(path)
files.sort()
path2 = (os.listdir('{}'.format(path)))
path2.sort(key=lambda x: int(x[:-4]))
# name="Directions 1.54138969.h5"
# img_data = h5py.File(name, 'w')
img = []
num = len(path2)
c = 1
for img_num in path2:
        if c != num:
            img_path = '{}\{}'.format(path, img_num)
            iimg = plt.imread(img_path)
            # print(iimg)
            img.append(iimg)
        c = c + 1

##创建h5文件并将图片列表写入
with h5py.File('Directions 1.54138969.h5', 'w') as f:
  f['image']= img

猜你喜欢

转载自blog.csdn.net/shuijiaobuzhundahulu/article/details/82953920