python - 将 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.
解决方案
如果您在 ipython 笔记本中运行,只需plt.show()
在代码末尾执行,它应该可以工作
推荐阅读
- javascript - 如何在样式化组件中键入 attrs 和样式道具 - React
- docker - 重启docker对k8s集群有什么影响
- r - 如何整理列是虚拟变量而单元格值是观察名称的数据集?
- javascript - 如何更改 mat-paginator 工具提示位置?
- javascript - OnChange 处理程序 ReactJS?
- php - 自定义搜索过滤器 - Yajra 数据表服务
- android - 在颤振中构建不成功
- python - 在 Service Desk JIRA 中使用 API 获取数据
- reactjs - 如何通过单击 React 功能组件中的按钮导航到不同的页面?
- angular - 无法运行现有的 Angular 项目 - 找不到本地工作区文件 ('angular.json')