Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display multiple images in one IPython Notebook cell?

If I have multiple images (loaded as NumPy arrays) how can I display the in one IPython Notebook cell?

I know that I can use plt.imshow(ima) to display one image… but I want to show more than one at a time.

I have tried:

 for ima in images:      display(Image(ima)) 

But I just get a broken image link:

enter image description here

like image 579
David Wolever Avatar asked Oct 19 '13 22:10

David Wolever


2 Answers

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.

like image 185
minrk Avatar answered Sep 20 '22 01:09

minrk


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) 
like image 24
dval Avatar answered Sep 19 '22 01:09

dval