sayem48
sayem48

Reputation: 101

Loading image data from .h5 file showing unusual color in Python 3.5 and h5py

I am using .h5 file to store lot of image data. Then resizing the image and store them in it.

creating dataset for image: t1=hdf5_file.create_dataset("train_img", train_shape, np.int8)

Loop over image address to resize and store them:

for i in range(len(train_addrs)):
    addr = train_addrs[i]
    img = cv2.imread(addr)
    img = cv2.resize(img, (128, 128), interpolation=cv2.INTER_CUBIC)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#save
    hdf5_file["train_img"][i, ...] = img[None]

hdf5_file.close()

When I try to check how the image looks with the following code:

hdf5_path = 'dataset.hdf5'
train_dataset = h5py.File(hdf5_path, "r")
train_set_x_orig = np.array(train_dataset["train_img"][:])
plt.imshow(train_set_x_orig[5]) #see 5th image
plt.show()

I get this unusual image. Top one from .h5 file, Bottom one is original image. I have checked shape of everything, they are fine. resize code in cv2 is also OK. Any kind of help will be appreciated.

After loading image from .h5 file

Original image

Upvotes: 1

Views: 1243

Answers (1)

hpaulj
hpaulj

Reputation: 231540

I load a b/w png with scipy.misc

In [1354]: arr2 = misc.imread('../Desktop/newworld2.png')
In [1355]: arr2.shape
Out[1355]: (500, 778, 3)
In [1356]: arr2.dtype
Out[1356]: dtype('uint8')

saving it with

In [1343]: d2 =f.create_dataset('newworld set',shape=(2, *arr2.shape), dtype=np.uint8)
In [1344]: d2[0]=arr2
In [1345]: d2[1]=arr2

preserved the original

saving as int8 produced the kind of color change that you show

In [1348]: d3 =f.create_dataset('newworldbad',shape=(2, *arr2.shape), dtype=np.int8)
In [1349]: d3[0]=arr2
In [1350]: d3[0]

white pixels, [255, 255, 255] changed to grey [127, 127, 127], etc.

Upvotes: 2

Related Questions