Skip to content Skip to sidebar Skip to footer

Scipy Imsave Saves Wrong Values

I'm trying to write code that will produce disparity maps using numpy and scipy, but the values that I store in my numpy array for my images are completely different from the value

Solution 1:

The data gets rescaled when the dtype of the array is changed from np.float64 (the data type of disp12im) to the 8 bit values stored in the image.

To avoid this, convert your image to data type np.uint8 before giving it to imsave:

misc.imsave('disp121.pgm', disp12im.astype(np.uint8))

For example, I'll save this x as a PGM image:

In [13]: x
Out[13]: 
array([[  1.,   3.,   5.],
       [ 21.,  23.,  25.]])

In [14]: x.dtype
Out[14]: dtype('float64')

Save x unaltered, and then read it back:

In [15]: imsave('foo.pgm', x)

In [16]: imread('foo.pgm')
Out[16]: 
array([[  0,  21,  42],
       [212, 234, 255]], dtype=uint8)

The values have been scaled up to the full 8-bit range.

Instead, convert x to np.uint8 before saving, and then read it back:

In [17]: imsave('foo.pgm', x.astype(np.uint8))

In [18]: imread('foo.pgm')
Out[18]: 
array([[ 1,  3,  5],
       [21, 23, 25]], dtype=uint8)

Post a Comment for "Scipy Imsave Saves Wrong Values"