Display multiple images in one IPython Notebook cell?
This is easier and works:
from IPython.display import Image
from IPython.display import display
x = Image(filename='1.png')
y = Image(filename='2.png')
display(x, y)
Horizontal layout
Short answer
plt.figure(figsize=(20,10))
columns = 5
for i, image in enumerate(images):
plt.subplot(len(images) / columns + 1, columns, i + 1)
plt.imshow(image)
Long answer
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline
images = []
for img_path in glob.glob('images/*.jpg'):
images.append(mpimg.imread(img_path))
plt.figure(figsize=(20,10))
columns = 5
for i, image in enumerate(images):
plt.subplot(len(images) / columns + 1, columns, i + 1)
plt.imshow(image)
Short answer:
call plt.figure()
to create new figures if you want more than one in a cell:
for ima in images:
plt.figure()
plt.imshow(ima)
But to clarify the confusion with Image
:
IPython.display.Image
is for displaying Image files, not array data. If you want to display numpy arrays with Image, you have to convert them to a file-format first (easiest with PIL):
from io import BytesIO
import PIL
from IPython.display import display, Image
def display_img_array(ima):
im = PIL.Image.fromarray(ima)
bio = BytesIO()
im.save(bio, format='png')
display(Image(bio.getvalue(), format='png'))
for ima in images:
display_img_array(ima)
A notebook illustrating both approaches.