Read and write images

Two easy ways: PIL and matplotlib (which uses PIL behind the scenes anyway). For PIL use: conda install pillow. Pillow is the new PIL.

matplotlib


In [1]:
import matplotlib.image as mpim

In [2]:
img = mpim.imread('../data/fluid.png')

We end up with a 4-channel array with (R,G,B) values in the range [0, 1]:


In [3]:
img


Out[3]:
array([[[ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        ..., 
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ]],

       [[ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        ..., 
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ]],

       [[ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        ..., 
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ]],

       ..., 
       [[ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        ..., 
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ]],

       [[ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        ..., 
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ]],

       [[ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        ..., 
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ],
        [ 0.        ,  0.        ,  0.57254905,  1.        ]]], dtype=float32)

plt.imshow() interprets the multiple channels as (R,G,B) values, so don't bother passing a colourmap:


In [4]:
import matplotlib.pyplot as plt
%matplotlib inline

plt.imshow(img, cmap='gray')
plt.show()


pillow

Even though you installed pillow, you use the PIL library. Weird, I know.


In [5]:
from PIL import Image

In [6]:
img = Image.open('../data/fluid.png')

The repr() of a PIL Image object renders the actual image in the notebook:


In [7]:
img


Out[7]:

In [8]:
import numpy as np

i = np.array(img)

This time the array is represented as uint8 values, so [0,255].


In [9]:
i


Out[9]:
array([[[  0,   0, 146, 255],
        [  0,   0, 146, 255],
        [  0,   0, 146, 255],
        ..., 
        [  0,   0, 146, 255],
        [  0,   0, 146, 255],
        [  0,   0, 146, 255]],

       [[  0,   0, 146, 255],
        [  0,   0, 146, 255],
        [  0,   0, 146, 255],
        ..., 
        [  0,   0, 146, 255],
        [  0,   0, 146, 255],
        [  0,   0, 146, 255]],

       [[  0,   0, 146, 255],
        [  0,   0, 146, 255],
        [  0,   0, 146, 255],
        ..., 
        [  0,   0, 146, 255],
        [  0,   0, 146, 255],
        [  0,   0, 146, 255]],

       ..., 
       [[  0,   0, 146, 255],
        [  0,   0, 146, 255],
        [  0,   0, 146, 255],
        ..., 
        [  0,   0, 146, 255],
        [  0,   0, 146, 255],
        [  0,   0, 146, 255]],

       [[  0,   0, 146, 255],
        [  0,   0, 146, 255],
        [  0,   0, 146, 255],
        ..., 
        [  0,   0, 146, 255],
        [  0,   0, 146, 255],
        [  0,   0, 146, 255]],

       [[  0,   0, 146, 255],
        [  0,   0, 146, 255],
        [  0,   0, 146, 255],
        ..., 
        [  0,   0, 146, 255],
        [  0,   0, 146, 255],
        [  0,   0, 146, 255]]], dtype=uint8)

In [ ]: