我需要将RGB图像数据(96 * 96 * 3)存储到HDF5数据存储中,与示例here中的示例相似,其中train_data大小为:(42000,1,28,28)。我的图片数据维度类似于(image_num,3,96,96)。将图像存储到hdf5商店?
这是我的代码存储一个图像到商店。
test_data = np.zeros((1 ,3,96,96))
try:
im = Image.open("/road8.jpg")
except:
print "failed to load image: %s" %("/road8.jpg")
img = im.resize((96, 96), Image.BILINEAR)
test_data[0,:,:,:] = np.array(img).astype(np.float32)/255
with h5py.File(script_dir+"/test.h5", 'w') as f:
f['data'] = test_data
f.close()
但是它给了我一个错误说“无法播放从形状(96,96,3)进入形状(3,96,96)输入数组”,我知道这意味着如果我初始化的数据等np.zeros((1,96,96,3)),它会没事的。不过,我需要的数据存储的顺序必须像np.zeros((1,3,96,96))。所以唯一的方法是在调整图像大小后进行转换。
我该怎么做?
我试过这个解决方案
img = im.resize((96, 96))
img = np.transpose(img, (2,0,1))
,但得到了一个错误说
Traceback (most recent call last):
File "vehicle_hdf5.py", line 51, in <module>
img = np.transpose(img, (2,0,1))
File "/Users/abc/anaconda/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 537, in transpose
return transpose(axes)
File "/Users/abc/anaconda/lib/python2.7/site-packages/PIL/Image.py", line 1936, in transpose
return self._new(self.im.transpose(method))
TypeError: an integer is required
是什么意思?
哪条线给你这个错误? –