Python reads tif image differently on Mac and Windows. Why? How? Which is correct? How to fix?
Question: I am trying to process some data stored as a tif image. To my dismay, python
2.7x reads it out differently on my Mac laptop and my Windows workstation.

    
    
    # import modules
    import numpy
    import matplotlib.pyplot as plt
    
    # read file
    image = plt.imread('fileName.tif')
    
    # display file as image
    plt.imshow(image)
    

A visual inspection does not reveal any significant differences, and all
notable features in one displayed image are visible in the other. The same
data is indeed being displayed in both cases. However, a closer look reveals
that there are important differences. The following code returns different
results on the two computers:

    
    
    image.shape   # shape & size of the array holding the data
    image.dtype   # data type of each element of the array
    image[0, 0]   # an individual pixel value
    image[1000, 1000]   # another pixel value
    image.min()
    image.max()
    

On Mac, that code returns

    
    
    (2048, 2048, 4)
    dtype('uint8')
    array([0, 0, 0, 255], dtype=uint8)
    array([71, 71, 71, 255], dtype=uint8)
    0
    255
    

Whereas on Windows, it returns

    
    
    (2048L, 2048L)
    dtype('uint16')
    0
    78
    0
    951
    

These differences may seem (mostly) trivial, but I'm working in a context
where such details are important.

I initially thought my Mac was interpreting the data correctly. If so, the
slice `image[:, :, n]` is the nth layer in a four-layer image. On Mac, layers
`0`, `1`, and `2` are identical, as the red, green, and blue channels would be
in a grayscale image, and layer `3` is all `255`s, as the opacity layer would
be in a fully opaque image.

> (Mistakes like this information redundancy are par for the course
> hereabouts. The data-taking setups here are cobbled together out of hardware
> from several different sources by people whose computer literacy often has
> room for improvement.)

However, an estimate of what the file size ought to be favors Windows. The
size of the file is the same to within 5% on the two computers, at about 8 MB;
I put the variation down to some difference in how the information is stored
by the two OSs. We estimate how large it ought to be:

Mac: 4 layers x 4x10^6 pixels per layer x 1 byte per pixel = approx. 16 MB

Windows: 1 layer x 4x10^6 pixels per layer x 2 bytes per pixel = approx. 8 MB

Since Python on Mac is claiming to read twice as much information as the file
size indicates, this suggests the Windows version is correct.

So my question is this: Which of the two readings is correct? How and why is
the same data being read differently? How can I ensure that similar data is
read correctly in the future, regardless of the system my code is run on?

[Link to image file used here.](https://office365stanford-
my.sharepoint.com/personal/apf_stanford_edu/_layouts/15/guestaccess.aspx?guestaccesstoken=jsz3eHdMO03ghIo6Mo2vjGVjKQmhr0gv9NgnMe6aaD8%3D&docid=090a8ff60906447eeb716ccf565291990&expiration=2016%2F04%2F24%2018%3A19%3A00)

Many thanks.


Answer: The very useful `tiffinfo` shows you have a one-channel, 16-bit image:

    
    
    $ tiffinfo sampleImage.tif 
    TIFFReadDirectory: Warning, Unknown field with tag 34710 (0x8796) encountered.
    TIFF Directory at offset 0x8 (8)
      Image Width: 2048 Image Length: 2048
      Resolution: 126.582, 126.582 pixels/cm
      Bits/Sample: 16
      Compression Scheme: None
      Photometric Interpretation: min-is-black
      Orientation: row 0 top, col 0 lhs
      Rows/Strip: 2048
      Planar Configuration: single image plane
      Tag 34710: 1024
    

Tag 34710 contains some camera information and shouldn't affect the result.

vips agrees that pixel (1000, 1000) is indeed 78, and the image maximum is
951.

    
    
    $ vips getpoint sampleImage.tif 1000 1000
    78 
    $ vips max sampleImage.tif 
    951.000000
    

So your Windows install is correct.

On Mac your image has been converted to 8-bit RGBA at some point in
processing. You'll need to start checking that the versions of the various
bits of software you are using are the same on both machines.

