[IPython-User] Calling 'Image' several times in the same cell

Matthias BUSSONNIER bussonniermatthias@gmail....
Tue Nov 13 14:49:33 CST 2012


Le 13 nov. 2012 à 21:18, Ariel Rokem a écrit :

> Hi everyone, 
> 
> Working in the notebook (on a recent github HEAD), I am trying to loop over a list of png file names and call Image on each one of them so that the output of that cell would ideally look something like: 
> 
> figure1 
> figure2
> 
> ...etc. 
> 
> I am doing that by calling the following code: 
> 
> from IPython.display import Image
> for l in os.listdir('.'):
>     if l.endswith('.png'):
>         Image(filename=l)
> 
> But I am getting no output. In a following cell, I just do: 
> 
> Image(filename=l)
> 
> And that works just fine - I get a display of the last file in that list. 
> 
> Any ideas? Thanks in advance! 

There is a difference between what is "displayed" and the "return value" of the cell. 
the return value is display after the "out" prompt, whereas the displayed value are before the out prompt. 

as in a for loop you would use print :

for i in range(10):
    print i

you will need to import display from IPython.display  and use it to actually display the images :

> from IPython.display import Image, display
> for l in os.listdir('.'):
>     if l.endswith('.png'):
>         display(Image(filename=l)) # added display


Does this make sense ? 
-- 
Matthias


More information about the IPython-User mailing list