Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is IPython.display.Image not showing in output?

Tags:

python

ipython

I have a cell that looks like this:

from IPython.display import Image
i = Image(filename='test.png')
i
print("test")

The output is just:

test

I don't see the image in the output. I checked to see that the file exists (anyway, if it did not exist, you get an error).

Any clues?

like image 768
yalis Avatar asked Feb 02 '16 05:02

yalis


2 Answers

creating the image with

i = Image(filename='test.png')

only creates the object to display. Objects are displayed by one of two actions:

  1. a direct call to IPython.display.display(obj), e.g.

    from IPython.display import display
    display(i)
    
  2. the displayhook, which automatically displays the result of the cell, which is to say putting i on the last line of the cell. The lone i in your example doesn't display because it is not the last thing in the cell. So while this doesn't display the image:

    i = Image(filename='test.png')
    i
    print("test")
    

    This would:

    i = Image(filename='test.png')
    print("test")
    i
    
like image 154
minrk Avatar answered Sep 21 '22 15:09

minrk


I had the same problem. Matplot lib expects to show figs outside the command line, for example in the GTK or QT.

Use this: get_ipython().magic(u'matplotlib inline')

It will enable inline backend for usage with IPython notebook.

See more here and here.

like image 29
Arthur Julião Avatar answered Sep 21 '22 15:09

Arthur Julião