我开始严重依赖IPython笔记本应用程序来开发和记录算法。这太棒了;但有些事情看起来应该是可能的,但我不知道该怎么做:

I would like to insert a local image into my (local) IPython notebook markdown to aid in documenting an algorithm. I know enough to add something like <img src="image.png"> to the markdown, but that is about as far as my knowledge goes. I assume I could put the image in the directory represented by 127.0.0.1:8888 (or some subdirectory) to be able to access it, but I can't figure out where that directory is. (I'm working on a mac.) So, is it possible to do what I'm trying to do without too much trouble?


当前回答

jupyter笔记本的最后一个版本接受复制/粘贴的图像本机

其他回答

我使用的是ipython 2.0,所以只有两行。

from IPython.display import Image
Image(filename='output1.png')

将图像导入Jupyter NB的操作比大多数人在这里提到的要简单得多。

简单地创建一个空Markdown单元格。 然后将图像文件拖放到空的Markdown单元格中。

然后出现插入图像的Markdown代码。

例如,下面以灰色突出显示的字符串将出现在Jupyter单元格中:

! [Venus_flytrap_taxonomy.jpg](附件:Venus_flytrap_taxonomy.jpg)

然后按Shift-Enter键执行Markdown单元格。然后Jupyter服务器将插入图像,然后图像将出现。

我正在运行Jupyter笔记本服务器是:5.7.4在Windows 7上使用Python 3.7.0。

这太简单了!!

更新截至2021年3月18日: 这种简单的“从windows文件系统中拖放”方法在JupyterLab中仍然可以正常工作。JupyterLab插入适当的HTML代码,将图像直接永久地嵌入到笔记本中,这样图像就存储在.ipynb文件中。我在Windows 10上运行JupyterLab v2.2.7, Python 3.7.9仍然可以在JupyterLab中运行。我在Windows 10上使用Python 3.7.9运行Jupyter Lab v2.2.7。

这在去年的Jupyter Classic Notebook v6.1.5中停止工作。我向Jupyter Classic Notebook开发人员报告了一个错误通知。

它再次在最新版本的Jupyter经典笔记本。我刚刚在2021年7月15日在v6.4中尝试了它。感谢Jupyter NB Classic Developers !!

明克的答案是正确的。

然而,我发现图像在打印视图中出现了损坏(在我的Windows机器上,在Chrome浏览器中运行IPython版本0.13.2的Anaconda发行版)

解决方法是使用<img src="../files/image.png">代替。

这使得图像在打印视图和正常的iPython编辑视图中都正确显示。

更新:当我升级到iPython v1.1.0时,不再需要这个解决方案,因为打印视图不再存在。事实上,您必须避免这种解决方法,因为它会阻止nbconvert工具查找文件。

(过时的)

IPython/Jupyter现在支持扩展模块,可以通过复制粘贴或拖放插入图像。

https://github.com/ipython-contrib/IPython-notebook-extensions

拖放扩展似乎在大多数浏览器中工作

https://github.com/ipython-contrib/IPython-notebook-extensions/tree/master/nbextensions/usability/dragdrop

但是复制粘贴只能在Chrome中使用。

在运行此代码之前,将默认块从“Code”更改为“Markdown”:

![<caption>](image_filename.png)

如果镜像文件在其他文件夹,可以执行以下操作:

![<caption>](folder/image_filename.png)