Quantcast

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

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Ariel Rokem-2
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! 

Ariel 


_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Calling 'Image' several times in the same cell

Matthias Bussonnier

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
_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Calling 'Image' several times in the same cell

Fernando Perez
In reply to this post by Ariel Rokem-2
On Tue, Nov 13, 2012 at 3:18 PM, Ariel Rokem <[hidden email]> wrote:
>
> Any ideas? Thanks in advance!

You should call display(Image(....)).

display() is the IPython equivalent of 'print' for the notebook.  By
default, we only call display on the *last* block of a cell, and  not
in a loop.  So just like

for i in range(10):
  i

wouldn't print you the numbers and instead you'd write

for i in range(10):
  print i

then you should do:

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

Cheers,

f
_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Calling 'Image' several times in the same cell

Fernando Perez
Argh! Gmail updated my inbox *after* I'd sent essentially the same
thing Matthias wrote ;)  Sorry for the dupe.
_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Calling 'Image' several times in the same cell

Ariel Rokem-2
In reply to this post by Fernando Perez
Hi Fernando and Matthias, 


On Tue, Nov 13, 2012 at 1:29 PM, Fernando Perez <[hidden email]> wrote:
On Tue, Nov 13, 2012 at 3:18 PM, Ariel Rokem <[hidden email]> wrote:
>
> Any ideas? Thanks in advance!

You should call display(Image(....)).

display() is the IPython equivalent of 'print' for the notebook.  By
default, we only call display on the *last* block of a cell, and  not
in a loop.  So just like

for i in range(10):
  i

wouldn't print you the numbers and instead you'd write

for i in range(10):
  print i

then you should do:

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

Cheers,


Thanks to both of you for the answers. It all makes sense and works great!

Cheers, 

Ariel  

_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user
Loading...