4

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?

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Is there some proper doc/tutorial on how to do image conversion and saving with PIL with respect to data type, precision and file format?
@Juh_, there are a few tutorials on PIL image conversion (just google for it), but not much that I'm aware regarding data types and precision. Most of the images are 24-bit (8 bits per channel, so uint8), and therefore the issue of precision and data type doesn't come often.
When googling, all I found are basic tutorials, or specific issues such as "16bits tiff". In my case I have (scientific) image data that I like to save in a viewable format (i.e. an image file), compressed if possible, but w/out loss of precision. I'd love to have a table of the best saving method for each case.
Sounds like you want lossless compression, which rules out most formats except png. Have you considered using fits? It is a very common scientific image data format (most telescopes write FITS files). You can use pyfits to interface with it in Python. It supports compression (gzip and rice, I think), but I haven't used it much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.