问题:将图像插入IPython Notebook Markdown

我开始严重依赖IPython Notebook应用程序来开发和记录算法。太棒了; 但是似乎有些应该可行的方法,但是我不知道该怎么做:

我想在我的(本地)IPython笔记本降价中插入本地图像,以帮助记录算法。我知道足够<img src="image.png">在降价中添加类似内容,但据我所知。我假设我可以将图像放在127.0.0.1:8888表示的目录(或某些子目录)中,以便能够访问它,但是我不知道该目录在哪里。(我正在Mac上工作。)那么,是否可以在没有太多麻烦的情况下做我想做的事情?

I am starting to depend heavily on the IPython notebook app to develop and document algorithms. It is awesome; but there is something that seems like it should be possible, but I can’t figure out how to do it:

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?


回答 0

笔记本目录中的文件位于“ files /” URL下。因此,如果它位于基本路径中,则为<img src="files/image.png">,子目录等也可用:<img src="files/subdir/image.png">,等等。

更新:从IPython 2.0开始,files/不再需要该前缀(请参阅发行说明)。因此,现在该解决方案<img src="image.png">可以按预期工作。

Files inside the notebook dir are available under a “files/” url. So if it’s in the base path, it would be <img src="files/image.png">, and subdirs etc. are also available: <img src="files/subdir/image.png">, etc.

Update: starting with IPython 2.0, the files/ prefix is no longer needed (cf. release notes). So now the solution <img src="image.png"> simply works as expected.


回答 1

到目前为止,大多数给出的答案都是错误的,建议加载其他库并使用代码而不是标记。在Ipython / Jupyter Notebooks中,这非常简单。确保单元格确实在标记中并显示图像以供使用:

![alt text](imagename.png "Title")

与建议的其他方法相比,进一步的优点是您可以显示所有常见的文件格式,包括jpg,png和gif(动画)。

Most of the answers given so far go in the wrong direction, suggesting to load additional libraries and use the code instead of markup. In Ipython/Jupyter Notebooks it is very simple. Make sure the cell is indeed in markup and to display a image use:

![alt text](imagename.png "Title")

Further advantage compared to the other methods proposed is that you can display all common file formats including jpg, png, and gif (animations).


回答 2

我正在使用ipython 2.0,所以只有两行。

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

I am using ipython 2.0, so just two line.

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

回答 3

[过时]

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

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

拖放扩展似乎可以在大多数浏览器中使用

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

但是复制和粘贴仅适用于Chrome。

[Obsolete]

IPython/Jupyter now has support for an extension modules that can insert images via copy and paste or drag & drop.

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

The drag & drop extension seems to work in most browsers

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

But copy and paste only works in Chrome.


回答 4

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

1)只需创建一个空的Markdown单元。2)然后将图像文件拖放到空白的Markdown单元格中。

然后出现将插入图像的降价代码。

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

![Venus_flytrap_taxonomy.jpg](attachment:Venus_flytrap_taxonomy.jpg)

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

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

这是如此简单!

Getting an image into Jupyter NB is a much simpler operation than most people have alluded to here.

1) Simply create an empty Markdown cell. 2) Then drag-and-drop the image file into the empty Markdown cell.

The Markdown code that will insert the image then appears.

For example, a string shown highlighted in gray below will appear in the Jupyter cell:

![Venus_flytrap_taxonomy.jpg](attachment:Venus_flytrap_taxonomy.jpg)

3) Then execute the Markdown cell by hitting Shift-Enter. The Jupyter server will then insert the image, and the image will then appear.

I am running Jupyter notebook server is: 5.7.4 with Python 3.7.0 on Windows 7.

This is so simple !!


回答 5

我将IPython笔记本与图像放在同一文件夹中。我使用Windows。图像名称是“ phuong huong xac dinh.PNG”。

在Markdown中:

<img src="phuong huong xac dinh.PNG">

码:

from IPython.display import Image
Image(filename='phuong huong xac dinh.PNG')

I put the IPython notebook in the same folder with the image. I use Windows. The image name is “phuong huong xac dinh.PNG”.

In Markdown:

<img src="phuong huong xac dinh.PNG">

Code:

from IPython.display import Image
Image(filename='phuong huong xac dinh.PNG')

回答 6

首先确保您在ipython笔记本单元格中处于markdown编辑模型中

这是其他人提出的方法的替代方法<img src="myimage.png">

![title](img/picture.png)

如果标题丢失,它似乎也可以工作:

![](img/picture.png)

请注意,路径中不应包含任何引号。不知道这是否适用于带有空格的路径!

First make sure you are in markdown edit model in the ipython notebook cell

This is an alternative way to the method proposed by others <img src="myimage.png">:

![title](img/picture.png)

It also seems to work if the title is missing:

![](img/picture.png)

Note no quotations should be in the path. Not sure if this works for paths with white spaces though!


回答 7

Jupyter Notebook的最新版本本机接受图像的复制/粘贴

Last version of jupyter notebook accepts copy/paste of image natively


回答 8

如果要在Markdown单元中显示图像,请使用:

<img src="files/image.png" width="800" height="400">

如果要在“代码”单元格中显示图像,请使用:

from IPython.display import Image
Image(filename='output1.png',width=800, height=400)

If you want to display the image in a Markdown cell then use:

<img src="files/image.png"  >

If you want to display the image in a Code cell then use:

from IPython.display import Image
Image(filename='output1.png',width=800, height=400)

回答 9

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

![<caption>](image_filename.png)

如果图像文件在另一个文件夹中,则可以执行以下操作:

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

Change the default block from “Code” to “Markdown” before running this code:

![<caption>](image_filename.png)

If image file is in another folder, you can do the following:

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

回答 10

对于那些希望将图像文件放在Jupyter机器上的位置的人,以便可以从本地文件系统显示它。

我把我的mypic.png

/root/Images/mypic.png

(即Jupyter在线文件浏览器中显示的Images文件夹)

在这种情况下,我需要将以下行放入Markdown单元中,以使我的图片显示在记事本中:

![My Title](Images/mypic.png)

For those looking where to place the image file on the Jupyter machine so that it could be shown from the local file system.

I put my mypic.png into

/root/Images/mypic.png

(that is the Images folder that shows up in the Jupyter online file browser)

In that case I need to put the following line into the Markdown cell to make my pic showing in the notepad:

![My Title](Images/mypic.png)

回答 11

明克的答案是正确的。

但是,我发现这些图像在“打印视图”中显得不完整(在运行Chrome浏览器中IPython 0.13.2版的Anaconda发行版的Windows计算机上)

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

这使得图像可以在“打印视图”和普通的iPython编辑视图中正确显示。

更新:自从我升级到iPython v1.1.0以来,由于打印视图不再存在,因此不再需要这种解决方法。实际上,您必须避免这种解决方法,因为它会阻止nbconvert工具查找文件。

minrk’s answer is right.

However, I found that the images appeared broken in Print View (on my Windows machine running the Anaconda distribution of IPython version 0.13.2 in a Chrome browser)

The workaround for this was to use <img src="../files/image.png"> instead.

This made the image appear correctly in both Print View and the normal iPython editing view.

UPDATE: as of my upgrade to iPython v1.1.0 there is no more need for this workaround since the print view no longer exists. In fact, you must avoid this workaround since it prevents the nbconvert tool from finding the files.


回答 12

您可以在jupyter笔记本中使用“ pwd”命令查找当前工作目录,不带引号。

You can find your current working directory by ‘pwd’ command in jupyter notebook without quotes.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。