图像另存为HDF5彩色 [英] Images saved as HDF5 arent colored

查看:182
本文介绍了图像另存为HDF5彩色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发将文本文件和jpg图像转换为HDF5-Format的程序.用HDFView 3.0打开后,图像似乎只保存为灰度.

Im currently working on a program that converts text files and jpg-images into the HDF5-Format. Opened with the HDFView 3.0, it seems that the Images are only saved in greyscales.

hdf = h5py.File("Sample.h5")
img = Image.open("Image.jpg")
data = np.asarray((img), dtype="uint8")
hdf.create_dataset("Photos/Image 1", data=data, dtype='uint8')

dset = hdf.get("Photos/Image 1")
dset.attrs['CLASS'] = 'IMAGE'
dset.attrs['IMAGE_VERSION'] = '1.2'
arr = np.asarray([0, 255], dtype=np.uint8)
dset.attrs['IMAGE_MINMAXRANGE'] = list(arr)
dset.attrs['IMAGE_SUBCLASS'] = 'IMAGE_TRUECOLOR'
dset.attrs['INTERLACE_MODE'] = 'INTERLACE_PIXEL'

在python中,可以使用Image.show()方法显示原始彩色图像:

In python it is possible to show the original colored image with the Image.show() method:

hdf = h5py.File("Sample.h5")
array = np.array(list(hdf.get("Photos/Image 1")))
img = Image.fromarray(array.astype('uint8'))
img.show()

推荐答案

问题的第一部分.

不要问我为什么,但是也许HDFview的维护者之一可以加强. 为了使HDFview能够正确显示图像,属性必须是有限长度的字符串才能正确解释.

Don't ask me why but maybe one of the maintainers of HDFview can step up. To enable HDFview to correctly display images the attributes must be finite length string to be correctly interpreted.

使用numpy软件包中的np.string_(<string>)

Use np.string_(<string>) from numpy package

import h5py  
import numpy as np
from PIL import Image

hdf = h5py.File("Sample.h5",'w')
img = Image.open("Image.jpg")
data = np.asarray((img), dtype="uint8")
hdf.create_dataset("Photos/Image 1", data=data, dtype='uint8')

dset = hdf.get("Photos/Image 1")
dset.attrs['CLASS'] = np.string_('IMAGE')
dset.attrs['IMAGE_VERSION'] = np.string_('1.2')
arr = np.asarray([0, 255], dtype=np.uint8)
dset.attrs['IMAGE_MINMAXRANGE'] = list(arr)
dset.attrs['IMAGE_SUBCLASS'] = np.string_('IMAGE_TRUECOLOR')
dset.attrs['INTERLACE_MODE'] = np.string_('INTERLACE_PIXEL')
hdf.close()

通过双击数据集图像1"进入HDFview

This gives in HDFview by double clicking on dataset "Image 1"

第二个问题.

我想您正在使用PIL软件包 函数fromarray需要图像模式",请参见

I suppose you are using the PIL package The function fromarray expects the "mode of the image" see https://pillow.readthedocs.io/en/3.1.x/handbook/concepts.html#concept-modes

您的情况是RBG

因此

import h5py
import numpy as np
from PIL import Image

hdf = h5py.File("Sample.h5",'r')
array = np.array(list(hdf.get("Photos/Image 1")))
img = Image.fromarray(array.astype('uint8'), 'RGB')
img.show()

会给你

这篇关于图像另存为HDF5彩色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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