Displaying Multiple Images in one cell

For David Wolever, who asked on SO.

In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

from IPython.display import display, Image

from glob import glob

Load a collection of image files with PIL into RGB numpy arrays, which can be displayed with plt.imshow

In [2]:
import PIL
images = [ PIL.Image.open(f) for f in glob('img/*') ]

def img2array(im):
    if im.mode != 'RGB':
        im = im.convert(mode='RGB')
    return np.fromstring(im.tostring(), dtype='uint8').reshape((im.size[1], im.size[0], 3))

np_images = [ img2array(im) for im in images ]

For multiple figures in one cell, just call plt.figure() for each new figure:

In [3]:
for img in np_images: