Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retain unchanged data when saving Numpy array to image with Scipy imsave

When saving a 2-dimensional Numpy array (of single values) with Scipy toimage or imsave the pixel values do not exactly match those in the Numpy array. Instead there are areas, mostly at edges, where the image algorithm seems use some sort of interpolation.

Is there an option to stop that interpolation and retain the exact data (e.g. 7 always gets rgb(7,7,7) in a PNG?

like image 614
Zardoz Avatar asked Feb 19 '23 04:02

Zardoz


1 Answers

If you have a 2D numpy array, then you are saving into a grayscale PNG so you never get an rgb image (only one channel). I'm not sure what you mean by single values, perhaps it is single precision floats? Although the PIL supports single precision floats, PNG does not. Saving to PNG you can either use 8-bits per channel (the default) or 16-bits per channel. This means that your array will be scaled to a maximum of 2^8/2^16 (8/16 bits), and converted to integer. It is in this conversion that results may vary slightly.

With scipy.misc.image there seems to be no option to save as 16-bit, so it will always write an 8-bit PNG. But you can use scipy.misc.toimage to create a 16-bit image, just be sure to pass mode='I'. Also be sure to specify the array min and max to avoid scaling. Here's how to use it to save a 16-bit png:

import numpy as np
import scipy.misc

a = np.random.uniform(0, 2**16 - 1, (500, 500)).astype('int32')
img = scipy.misc.toimage(a, high=np.max(a), low=np.min(a), mode='I')
img.save('my16bit.png')

# check that you got the same values
b = scipy.misc.imread('my16bit.png')
b.dtype
# dtype('int32')
np.array_equal(a, b)
# True

Note that in this example I used int32 for data type. However, the data must still fit in a uint16. If you put negative values or values larger than 2^16, those will be clipped in the save to PNG. Conversely, even though sp.misc.imread reads as int32, the data will never be more than uint16.

In summary: if you want to write exactly the same numpy array to a PNG you need to make sure it is of uint8/uint16 type, and that you pass the correct high/low/mode to scipy.misc.toimage.

like image 60
tiago Avatar answered Feb 20 '23 18:02

tiago