簡體   English   中英

如何可視化 h5 格式數據的圖像?

[英]How can I visualise an image in h5 format data?

import h5py
f = h5py.File('the_file.h5', 'r')
one_data = f['key']
print(one_data.shape)
print(one_data.dtype)
print(one_data)

我使用上面的代碼打印信息。 打印結果為:

(320, 320, 3)
uint8
<HDF5 dataset "1458552843.750": shape (320, 320, 3), type "|u1">
import cv2
import numpy as np
import h5py
f = h5py.File('the_file.h5', 'r')
dset = f['key']
data = np.array(dset[:,:,:])
file = 'test.jpg'
cv2.imwrite(file, data)

jet提供的解決方案工作正常,但缺點是需要包含OpenCV(cv2)。 如果您沒有將OpenCV用於其他任何事情,安裝/包含它僅僅是為了保存文件有點過分。 或者,您可以使用imageio.imwritedoc ),例如:

import imageio
import numpy as np
import h5py

f = h5py.File('the_file.h5', 'r')
dset = f['key']
data = np.array(dset[:,:,:])
file = 'test.png' # or .jpg
imageio.imwrite(file, data)

安裝imageio就像pip install imageio一樣簡單。

此外, matplotlib.image.imsavedoc )提供了類似的圖像保存功能。

它可以更簡單:

pip install Pillow h5py

然后

import h5py
from PIL import Image

f = h5py.File('the_file.h5', 'r')
dset = f['key'][:]
img = Image.fromarray(dset.astype("uint8"), "RGB")
img.save("test.png")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM