如何在 ipython 笔记本中重新加载图像?

How to reload image in ipython notebook?

在我的笔记本中,我可以像这样在同一文件夹中以降价显示图像:

<img src="files/adaptive_filter.png" alt="Schema of adaptive filter" height="100"> 

如果我在 src 中使用不带 files/ 的代码,它将不起作用。

现在我更改了图像,ipython 笔记本仍然显示原始图像。我尝试从代码中删除它并重新启动笔记本,但没有帮助。

我该怎么办?图片是否存储在某处?

提前致谢。

我也 运行 遇到了这个问题,我使用自己的 class 输出了一些 python 图并将它们嵌入到 IPython 笔记本中.解决这个问题的一种 hack 方法是在图像 url 的末尾添加一个 运行dom 参数。例如

<img src="files/adaptive_filter.png?1" alt="Schema of adaptive filter" height="100">

不会缓存在与

相同的位置
<img src="files/adaptive_filter.png?2" alt="Schema of adaptive filter" height="100">

一种编程方式是通过 python 包含图片,而不是 markdown,例如:

# pick a random integer with 1 in 2 billion chance of getting the same
# integer twice
import random
__counter__ = random.randint(0,2e9)

# now use IPython's rich display to display the html image with the
# new argument
from IPython.display import HTML, display
display(HTML('<img src="files/adaptive_filter.png?%d" ' +
             'alt="Schema of adaptive filter" ' +
             'height="100">' % __counter__))

每次 运行 代码单元格

都应该更新图像

我运行陷入完全一样的问题。以下程序对我有用:

在您的 .ipynb 文件所在的文件夹中,有一个名为 .ipynb_checkpoints/ 的缓存目录。该缓存目录中应该有一个文件与您正在处理的文件具有相同的文件名。现在 remove/delete .ipynb_checkpoint/ 目录中的缓存文件,然后重新加载浏览器。您应该能够看到更新后的图像。

我的环境:macOS 10.14.2,Chrome浏览器71.0,通过anaconda安装Jupyter 1.0.0

希望对您有所帮助。

  1. 在ipynb文件目录下,使用ls命令,会显示一个隐藏的 目录 .ipynb_checkpoints

  2. 删除.ipynb_checkpoints目录,然后重新运行单元格 代码,你会看到最新的图片。

我在 jupyter 中遇到了同样的图像缓存问题,并且在使用 matplotlib.pyplot 时修改文件时没有更新。我使用 IPython.display.Image

修复了它
from IPython.display import Image
def saveImage(path, show=True): 
    plt.savefig(path, facecolor="white", bbox_inches='tight') 
    if show: return Image(path)

然后在代码单元格的末尾,类似saveImage('./someImage.png')的内容将保存并显示图像。

同样 Image('./someImage.png') 将显示磁盘中的图像(不保存)。

这似乎避免了缓存问题,并且图像在 PDF 导出中正确显示(另请参阅 以了解其基于的答案)。

就我而言,它有助于减小 jupyter notebook 的大小,防止其在呈现和更新 matplotlib.pyplot 图表时崩溃。