首页 > 解决方案 > 将 JPG 转换为 H5,加载和绘图

问题描述

我正在尝试将 JPG 文件放入 H5,并为此编写了一个脚本。使用 PIL 加载图像,然后写入 H5 文件,然后出于测试目的,我读取了 H5 文件。

图像已加载,类型似乎正确,您发现任何错误吗?

import h5py
import numpy as np
from glob import glob # import "submodule"
from PIL import Image
import matplotlib.pyplot as plt

# img source directory
src = "images/raw/cats/*"

# self explanatory
outfile = "images/clean/cat.hdf5" 


listOfImages = glob(src)

# img loaded as JpegImageFile Object
oneImage = Image.open(listOfImages[0])

# open file
f = h5py.File(outfile, "w")
data  =  np.array(oneImage) #this is an array 200x200x3

# save dataset (ndarray but with more features)
dset = f.create_dataset("images", data=data) # array 200x200x3

# Check if files can be read from the h5py file
with h5py.File(outfile, "r") as f:
    image = f['images']
    print(image.shape, image.dtype)
    plt.imshow(image)

# outout:::: (200, 200, 3) uint8, but nothing is plt.

标签: pythonimagemachine-learning

解决方案


如果您在 ipython 笔记本中运行,只需plt.show() 在代码末尾执行,它应该可以工作


推荐阅读