How to read a 32-bit integer image using matplotlib?
<p>I need to read a <strong>single-channel 32-bit integer TIFF image</strong> from disk using Python 2 to perform some image analysis. I tried <a href="http://matplotlib.org/api/image_api.html?highlight=imread#matplotlib.image.imread" rel="nofollow noreferrer"><code>image.imread</code></a> from matplotlib but I was unable to get the code to work because data were read as 4-channel 8-bit integer image:</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; import matplotlib.image as mpimg
&gt;&gt;&gt; img = mpimg.imread('my_image.tif')
&gt;&gt;&gt; img.shape
(52, 80, 4)
&gt;&gt;&gt; img[0:2, 0:2]
array([[[255, 255, 255, 255],
        [255, 255, 255, 255]],

       [[255, 255, 255, 255],
        [255, 255, 255, 255]]], dtype=uint8)
</code></pre>

<p><strong>QUESTION</strong>: Is it possible to read a single-channel 32-bit integer image using matplotlib?</p>

<p>I'm aware there are alternative ways to read such an image in Python, for example using <a href="http://effbot.org/imagingbook/image.htm#tag-Image.open" rel="nofollow noreferrer"><code>Image.open</code></a> from PIL:</p>

<pre><code>&gt;&gt;&gt; from PIL import Image
&gt;&gt;&gt; img = np.asarray(Image.open('my_image.tif'))
&gt;&gt;&gt; img.dtype
dtype('int32')
&gt;&gt;&gt; img.shape
(52, 80)
&gt;&gt;&gt; img[0:2, 0:2]
array([[8745, 8917],
       [8918, 9479]])
</code></pre>

<p>Another possibility would be using <a href="http://scikit-image.org/docs/dev/api/skimage.io.html#skimage.io.imread" rel="nofollow noreferrer"><code>io.imread</code></a> from scikit-learn:</p>

<pre><code>&gt;&gt;&gt; from skimage import io
&gt;&gt;&gt; img = io.imread('my_image.tif')
</code></pre>

<p>Yet another approach is to utilize the <a href="http://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html?highlight=imread#cv2.imread" rel="nofollow noreferrer"><code>imread</code></a> function from OpenCV. But in this case data have to be casted to 32-bit integer:</p>

<pre><code>&gt;&gt;&gt; import cv2
&gt;&gt;&gt; img = cv2.imread('my_image.tif', -1)
&gt;&gt;&gt; img[0:2, 0:2]
array([[  1.22543551e-41,   1.24953784e-41],
       [  1.24967797e-41,   1.32829081e-41]], dtype=float32)
&gt;&gt;&gt; img.dtype = np.int32
&gt;&gt;&gt; img[0:2, 0:2]
array([[8745, 8917],
       [8918, 9479]])
</code></pre>