使用h5py将栅格图像添加到HDF5文件 [英] Add raster image to HDF5 file using h5py

查看:124
本文介绍了使用h5py将栅格图像添加到HDF5文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这是一个新手问题,我深表歉意,但是我对Python和HDF5还是很陌生.我正在使用h5py,numpy和Python 2.7.我有来自各种文件的数据,需要将这些数据导入到一个HDF5文件中.每个文件中的数据将存储在不同的组中.这些组中的每一个都需要包含1)来自文件的原始数据(作为m x n矩阵)和2)根据规范化的原始数据生成的图像栅格.

I apologize if this is sort of a newbie question, but I am fairly new to Python and HDF5. I am using h5py, numpy, and Python 2.7. I have data from various files that need to be imported into one HDF5 file. The data from each file is to be stored in a different group. Each of these groups needs to contain 1) the raw data from the file as an m x n matrix and 2) an image raster generated from normalized raw data.

我能够完成第1部分,并且能够规范化数据,但是由于我不知道如何将光栅图像添加到组中,因此无法将规范化的数据写入光栅图像.似乎应该有一种简单直接的方法来执行此操作,但是我已经阅读了文档,但没有找到一个.如何在h5py中执行此操作,如果无法使用h5py完成该操作,该怎么办?

I am able to accomplish part 1, and am able to normalize the data, but I am not able to write this normalized data to a raster image because I don't know how to add a raster image to a group. It seems like there should be an easy, straightforward way to do this, but I have read the documentation and haven't found one. How would one do this in h5py, and if it can't be done using h5py, what should I use to accomplish this?

谢谢!

推荐答案

HDF5中的图像没有什么特别的.您提供的链接用于高级库绑定.您可以轻松使用HDF5中图像的规范,只是属性.

There is nothing special about images in HDF5. The link you provided is for the high level library bindings. You can just as easily use the specifications of images in HDF5, which are just attributes.

这是一个非常快速而肮脏的示例:

Here is a very quick and dirty example:

#!/usr/bin/env python

import numpy as np
import h5py

# Define a color palette
pal =  np.array([[0,     0, 168],
                 [0,     0, 252],
                 [0,   168, 252],
                 [84,  252, 252],
                 [168, 252, 168],
                 [0,   252, 168],
                 [252, 252,  84],
                 [252, 168,   0],
                 [252,   0,   0]],
                 dtype=np.uint8
               )

# Generate some data/image
x = np.linspace(0,pal.shape[0]-1)
data,Y = np.meshgrid(x,x)

# Create the HDF5 file
f = h5py.File('test.h5', 'w')

# Create the image and palette dataspaces
dset = f.create_dataset('img', data=data)
pset = f.create_dataset('palette', data=pal)

# Set the image attributes
dset.attrs['CLASS'] = 'IMAGE'
dset.attrs['IMAGE_VERSION'] = '1.2'
dset.attrs['IMAGE_SUBCLASS'] =  'IMAGE_INDEXED'
dset.attrs['IMAGE_MINMAXRANGE'] = np.array([0,255], dtype=np.uint8)
dset.attrs['PALETTE'] = pset.ref

# Set the palette attributes
pset.attrs['CLASS'] = 'PALETTE'
pset.attrs['PAL_VERSION'] = '1.2'
pset.attrs['PAL_COLORMODEL'] = 'RGB'
pset.attrs['PAL_TYPE'] = 'STANDARD8'

# Close the file
f.close()


运行示例,然后在HDFView中查看图像:


Run the example and then look at the image in HDFView:

请注意,您必须使用打开方式"打开图像数据才能将其视为图像,因为默认情况下是表格视图.

Note that you have to open the image data with "Open As" in order to see it as an image, as the table view is the default.

这篇关于使用h5py将栅格图像添加到HDF5文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆