yalis yalis - 1 month ago 9
Python Question

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

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?

Answer

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