1

I need to work with some greyscale tif files and I have been using PIL to import them as images and convert them into numpy arrays:

    np.array(Image.open(src))

I want to have a transparent understanding of exactly what the values of these array correspond to and in particular, it was not clear what value was appropriate as a white point or black point for my images. For instance if I wanted to convert this array into an array of floats with pixel values of 1 for white values and 0 for black with other values scaled linearly in between.

I have tried some naive methods including scaling by the maximum value in the array but opening the resulting files, there is always some amount of shift in the color levels.

Is there any documentation for the proper way to understand the values stored in these tif arrays?

1 Answer 1

1

A TIFF is basically a computer file format for storing raster graphics images. It has a lot of specs and quick search on the web will get you the resources you need.

The thing is you are using PIL as your input library. The array you have is likely working with an uint8 data type, which means your data can be anywhere within 0 to 255. To obtain the 0 to 1 color range do the following:

im = np.array(Image.open(src)).astype('float32')/255

Notice your array will likely have 4 layers given in the third dimension im[:,:, here] (im.shape = (i,j,k)). So each trace im[i,j,:] (which represents a pixel) is going to be a quadruplet for an RGBA value.

The R stands for Red (or quantity of Red), G for Green, B for Blue. A is the alpha channel and it is what enables you to have transparency (lower values means less opacity and more transparency).

It can also have three layers for only RGB, or one layer if intended to be plotted in the grey-scale.

In the case you have RGB (or RGBA but not considering alpha) but need a single value you should understand that there are quite a few different ways of doing this. In this post @denis recommends the use of the following formulation:

Y = .2126 * R^gamma + .7152 * G^gamma + .0722 * B^gamma

where gamma is 2.2 for many PCs. The usual R G B are sometimes written as R' G' B' (R' = Rlin ^ (1/gamma)) (purists tongue-click) but here I'll drop the '.

And finally L* = 116 * Y ^ 1/3 - 16 to obtain the luminance.

I recommend you to read his post. Also consider looking into the following concepts:

2
  • Thanks for your answer. One thing seems strange to me here and that is that my numpy array is pretty clearly not a uint8 array. The maximum values in the array are somewhere in the order of magnitude of 30000 though to be fair, I am working with 16 bit greyscale tifs.
    – Frank Wang
    Jun 11, 2016 at 14:57
  • @FrankWang That explains it than. That is an old problem with PIL which I would assume it would be solved by now. In any case there are solutions. Check for example this link: blog.philippklaus.de/2011/08/handle-16bit-tiff-images-in-python
    – armatita
    Jun 11, 2016 at 16:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.