为什么 IPython.display.Image 没有显示在输出中?

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

我有一个看起来像这样的单元格:

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

输出只是:

test

我没有在输出中看到图像。我检查了该文件是否存在(无论如何,如果它不存在,您会得到一个错误)。

有什么线索吗?

使用

创建图像
i = Image(filename='test.png')

只创建要显示的对象。对象通过以下两个操作之一显示:

  1. 直接调用 IPython.display.display(obj),例如

    from IPython.display import display
    display(i)
    
  2. displayhook,自动显示单元格的结果,也就是说把i放在单元格的最后一行。您的示例中单独的 i 不会显示,因为它不是单元格中的最后一件事。所以虽然这不显示图像:

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

    这将:

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

我遇到了同样的问题。 Matplot lib 希望在命令行之外显示无花果,例如在 GTK 或 QT 中。

使用这个:get_ipython().magic(u'matplotlib inline')

它将启用内联后端以用于 IPython 笔记本。

查看更多here and here.

查看代码后,我可以说,在 Windows 下,从 IPython 7.1.0 开始,这仅受 %matplotlib inline 支持,在 %matplotlib inline 下不起作用互动 IPython shell。

Jupyter 中有额外的代码。以下示例适用于

  • jupyter qtconsole
  • jupyter notebook
  • jupyter console原则上,但只能通过外部程序,然后例如临时文件被删除

def test_display(): import pyx c = pyx.canvas.canvas() circle = pyx.path.circle(0, 0, 2) c.stroke(circle, [pyx.style.linewidth.Thick,pyx.color.rgb.red]) return c display(test_display())