Frank Wang
Frank Wang

Reputation: 191

Interpreting numpy array obtained from tif file

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?

Upvotes: 1

Views: 1884

Answers (1)

armatita
armatita

Reputation: 13465

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:

Upvotes: 1

Related Questions