问题:如何从本地计算机或Web资源将图像或图片嵌入jupyter笔记本中?

我想将图像包括在Jupyter笔记本中。

如果我执行以下操作,则它会起作用:

from IPython.display import Image
Image("img/picture.png")

但是我想将图像包含在markdown单元格中,以下代码给出404错误:

![title]("img/picture.png")

我也试过

![texte]("http://localhost:8888/img/picture.png")

但是我仍然得到同样的错误:

404 GET /notebooks/%22/home/user/folder/img/picture.png%22 (127.0.0.1) 2.74ms referer=http://localhost:8888/notebooks/notebook.ipynb

I would like to include image in a jupyter notebook.

If I did the following, it works :

from IPython.display import Image
Image("img/picture.png")

But I would like to include the images in a markdown cell and the following code gives a 404 error :

![title]("img/picture.png")

I also tried

![texte]("http://localhost:8888/img/picture.png")

But I still get the same error :

404 GET /notebooks/%22/home/user/folder/img/picture.png%22 (127.0.0.1) 2.74ms referer=http://localhost:8888/notebooks/notebook.ipynb

回答 0

在markdown中,不得在图像文件名称的前后加上引号!

如果您仔细阅读错误消息,您将%22在链接中看到两个部分。那是html编码的引号。

你必须换线

![title]("img/picture.png")

![title](img/picture.png)

更新

假定您具有以下文件结构,并且您jupyter notebook在存储文件的目录example.ipynb(<-包含映像的标记)中运行 命令:

/
+-- example.ipynb
+-- img
    +-- picture.png

You mustn’t use quotation marks around the name of the image files in markdown!

If you carefully read your error message, you will see the two %22 parts in the link. That is the html encoded quotation mark.

You have to change the line

![title]("img/picture.png")

to

![title](img/picture.png)

UPDATE

It is assumed, that you have the following file structure and that you run the jupyter notebook command in the directory where the file example.ipynb (<– contains the markdown for the image) is stored:

/
+-- example.ipynb
+-- img
    +-- picture.png

回答 1

有几种方法可以在Jupyter笔记本中发布图像:

通过HTML:

from IPython.display import Image
from IPython.core.display import HTML 
Image(url= "http://my_site.com/my_picture.jpg")

您保留使用HTML标签调整大小等的功能。

Image(url= "http://my_site.com/my_picture.jpg", width=100, height=100)

您还可以通过相对或绝对路径显示本地存储的图像。

PATH = "/Users/reblochonMasque/Documents/Drawings/"
Image(filename = PATH + "My_picture.jpg", width=100, height=100)

如果图像宽于显示设置: 谢谢

用于unconfined=True禁用图像的最大宽度限制

from IPython.core.display import Image, display
display(Image('https://i.ytimg.com/vi/j22DmsZEv30/maxresdefault.jpg', width=1900, unconfined=True))

或通过降价:

  • 确保该单元格是降价单元格,而不是代码单元格,感谢@游凯超在评论中)
  • 请注意,在某些系统上,降价标记不允许在文件名中使用空格。感谢评论中的@CoffeeTableEspresso和@zebralamy)
    (在macOS上,只要您位于降价单元格上,您就可以这样做:![title](../image 1.png),而不必担心空白)。

对于网络图像:

![Image of Yaktocat](https://octodex.github.com/images/yaktocat.png)

如@cristianmtr所示。请注意不要同时使用这些引号""''网址中的引号。

或本地的:

![title](img/picture.png)

由@Sebastian演示

There are several ways to post an image in Jupyter notebooks:

via HTML:

from IPython.display import Image
from IPython.core.display import HTML 
Image(url= "http://my_site.com/my_picture.jpg")

You retain the ability to use HTML tags to resize, etc…

Image(url= "http://my_site.com/my_picture.jpg", width=100, height=100)

You can also display images stored locally, either via relative or absolute path.

PATH = "/Users/reblochonMasque/Documents/Drawings/"
Image(filename = PATH + "My_picture.jpg", width=100, height=100)

if the image it wider than the display settings: thanks

use unconfined=True to disable max-width confinement of the image

from IPython.core.display import Image, display
display(Image('https://i.ytimg.com/vi/j22DmsZEv30/maxresdefault.jpg', width=1900, unconfined=True))

or via markdown:

  • make sure the cell is a markdown cell, and not a code cell, thanks @游凯超 in the comments)
  • Please note that on some systems, the markdown does not allow white space in the filenames. Thanks to @CoffeeTableEspresso and @zebralamy in the comments)
    (On macos, as long as you are on a markdown cell you would do like this: ![title](../image 1.png), and not worry about the white space).

for a web image:

![Image of Yaktocat](https://octodex.github.com/images/yaktocat.png)

as shown by @cristianmtr Paying attention not to use either these quotes "" or those '' around the url.

or a local one:

![title](img/picture.png)

demonstrated by @Sebastian


回答 2

另外,您可以使用纯HTML <img src>,它允许您更改高度和宽度,并仍由markdown解释器读取:

<img src="subdirectory/MyImage.png" width=60 height=60 />

Alternatively, you can use a plain HTML <img src>, which allows you to change height and width and is still read by the markdown interpreter:

<img src="subdirectory/MyImage.png" width=60 height=60 />

回答 3

我知道这并不完全相关,但是由于当您搜索“ 如何在Jupyter中显示图像 ”时,此答案多次排名第一,因此也请考虑此答案。

您可以使用matplotlib如下显示图像。

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread("your_image.png")
plt.imshow(image)
plt.show()

I know this is not fully relevant, but since this answer is ranked first many a times when you search ‘how to display images in Jupyter‘, please consider this answer as well.

You could use matplotlib to show an image as follows.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = mpimg.imread("your_image.png")
plt.imshow(image)
plt.show()

回答 4

我很惊讶这里没有人提到html cell magic选项。来自文档(IPython,但与Jupyter相同)

%% html

Render the cell as a block of HTML

I’m surprised no one here has mentioned the html cell magic option. from the docs (IPython, but same for Jupyter)

%%html

Render the cell as a block of HTML

回答 5

除了使用HTML的其他答案(在Markdown中或使用%%HTML魔术:

如果您需要指定图像高度,则将无法使用:

<img src="image.png" height=50> <-- will not work

这是因为Jupyter中的CSS样式height: auto默认情况下会使用img标签来覆盖HTML高度属性。您需要改写CSS height属性:

<img src="image.png" style="height:50px"> <-- works

In addition to the other answers using HTML (either in Markdown or using the %%HTML magic:

If you need to specify the image height, this will not work:

<img src="image.png" height=50> <-- will not work

That is because the CSS styling in Jupyter uses height: auto per default for the img tags, which overrides the HTML height attribute. You need need to overwrite the CSS height attribute instead:

<img src="image.png" style="height:50px"> <-- works

回答 6

将图像直接插入Jupyter笔记本中。

注意:您应该在计算机上拥有图像的本地副本

您可以将图像插入Jupyter笔记本本身。这样,您无需将图像单独保存在文件夹中。

脚步:

  1. 将单元格转换为markdown

    • 在所选单元格上按M

    • 在菜单栏中,单元格>单元格类型>降价。
      注意:将单元格转换为Markdown非常重要,否则,第2步中的“插入图片”选项将无效)
  2. 现在转到菜单栏,然后​​选择编辑->插入图像。

  3. 从磁盘中选择图像并上传。

  4. Ctrl+ EnterShift+ Enter

这将使图像成为笔记本的一部分,您无需在目录或Github中上传。我觉得这看起来更干净,而且不容易出现URL损坏的问题。

Insert the image directly in the Jupyter notebook.

Note: You should have a local copy of the image on your computer

You can insert the image in the Jupyter notebook itself. This way you don’t need to keep the image separately in the folder.

Steps:

  1. Convert the cell to markdown by:

    • pressing M on the selected cell
      OR
    • From menu bar, Cell > Cell Type > Markdown.
      (Note: It’s important to convert the cell to Markdown, otherwise the “Insert Image” option in Step 2 will not be active)
  2. Now go to menu bar and select Edit -> Insert Image.

  3. Select image from your disk and upload.

  4. Press Ctrl+Enter or Shift+Enter.

This will make the image as part of the notebook and you don’t need to upload in the directory or Github. I feel this looks more clean and not prone to broken URL issue.


回答 7

使用Markdown的方法如下:

![Image of Yaktocat](https://octodex.github.com/images/yaktocat.png)

Here’s how you can do it with Markdown:

![Image of Yaktocat](https://octodex.github.com/images/yaktocat.png)

回答 8

  1. 将单元格模式设置为降价
  2. 将图像拖放到单元格中。将创建以下命令:

![image.png](attachment:image.png)

  1. 执行/运行单元格,图像出现。

该图像实际上是嵌入在ipynb笔记本中的,您无需弄乱单独的文件。不幸的是,这还不适用于Jupyter-Lab(v 1.1.4)。

编辑:在JupyterLab版本1.2.6中工作

  1. Set cell mode to Markdown
  2. Drag and drop your image into the cell. The following command will be created:

![image.png](attachment:image.png)

  1. Execute/Run the cell and the image shows up.

The image is actually embedded in the ipynb Notebook and you don’t need to mess around with separate files. This is unfortunately not working with Jupyter-Lab (v 1.1.4) yet.

Edit: Works in JupyterLab Version 1.2.6


回答 9

如果要使用Jupyter Notebook API(现在不再使用IPython),则可以找到ipywidgets Jupyter的子项目。您有一个Image小部件。Docstring指定您有value一个字节参数。因此,您可以执行以下操作:

import requests
from ipywidgets import Image

Image(value=requests.get('https://octodex.github.com/images/yaktocat.png').content)

我同意,使用Markdown样式更简单。但是它向您显示了图像显示Notebook API。您还可以使用widthheight参数调整图像的大小。

If you want to use the Jupyter Notebook API (and not the IPython one anymore), I find the ipywidgets Jupyter’s sub-project. You have an Image widget. Docstring specifies that you have a value parameter which is a bytes. So you can do:

import requests
from ipywidgets import Image

Image(value=requests.get('https://octodex.github.com/images/yaktocat.png').content)

I agree, it’s simpler to use the Markdown style. But it shows you the Image display Notebook API. You can also resize the image with the width and height parameters.


回答 10

这是JupyterPython3的解决方案:

我将图像放在名为的文件夹中ImageTest。我的目录是:

C:\Users\MyPcName\ImageTest\image.png

为了显示图像,我使用了以下表达式:

![title](/notebooks/ImageTest/image.png "ShowMyImage")

还要注意/\

Here is a Solution for Jupyter and Python3:

I droped my images in a folder named ImageTest. My directory is:

C:\Users\MyPcName\ImageTest\image.png

To show the image I used this expression:

![title](/notebooks/ImageTest/image.png "ShowMyImage")

Also watch out for / and \


回答 11

这在降价单元中对我有用。无论如何,如果图像或简单文件,我都无需特别提及。

![](files/picture.png)

This works for me in a markdown cell. Somehow I do not need to mention specifically if its an image or a simple file.

![](files/picture.png)

回答 12

我发现的一件事是,图像的路径必须与笔记本计算机最初加载的位置有关。如果您将CD转到其他目录,例如“图片”,则Markdown路径仍相对于原始加载目录。

One thing I found is the path of your image must be relative to wherever the notebook was originally loaded from. if you cd to a different directory, such as Pictures your Markdown path is still relative to the original loading directory.


回答 13

同意,我遇到了同样的问题,这是可行的,而没有奏效的:

WORKED: <img src="Docs/pinoutDOIT32devkitv1.png" width="800"/>
*DOES NOT WORK: <img src="/Docs/pinoutDOIT32devkitv1.png" width="800"/>
DOES NOT WORK: <img src="./Docs/pinoutDOIT32devkitv1.png" width="800"/>*

Agreed, i had the same issues and this is what worked and what did not:

WORKED: <img src="Docs/pinoutDOIT32devkitv1.png" />
*DOES NOT WORK: <img src="/Docs/pinoutDOIT32devkitv1.png" />
DOES NOT WORK: <img src="./Docs/pinoutDOIT32devkitv1.png" />*

回答 14

尽管上面的许多答案都提供了使用文件或Python代码嵌入图像的方法,但是有一种方法可以仅使用markdown和base64将图像嵌入jupyter笔记本本身!

要在浏览器中查看图像,您可以访问data:image/png;base64,**image data here**以base64编码的PNG图像或data:image/jpg;base64,**image data here**以base64编码的JPG图像的链接。在此答案的末尾可以找到一个示例链接。

要将其嵌入到markdown页面中,只需使用与文件Answers类似的结构,但要使用base64链接:![**description**](data:image/**type**;base64,**base64 data**)。现在,您的图像已100%嵌入到Jupyter Notebook文件中!

示例链接: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABHNCSVQICAgIfAhkiAAAAD9JREFUGJW1jzEOADAIAqHx/1+mE4ltNXEpI3eJQknCIGsiHSLJB+aO/06PxOo/x2wBgKR2jCeEy0rOO6MDdzYQJRcVkl1NggAAAABJRU5ErkJggg==

降价示例: ![smile](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABHNCSVQICAgIfAhkiAAAAD9JREFUGJW1jzEOADAIAqHx/1+mE4ltNXEpI3eJQknCIGsiHSLJB+aO/06PxOo/x2wBgKR2jCeEy0rOO6MDdzYQJRcVkl1NggAAAABJRU5ErkJggg==)

While a lot of the above answers give ways to embed an image using a file or with Python code, there is a way to embed an image in the jupyter notebook itself using only markdown and base64!

To view an image in the browser, you can visit the link data:image/png;base64,**image data here** for a base64-encoded PNG image, or data:image/jpg;base64,**image data here** for a base64-encoded JPG image. An example link can be found at the end of this answer.

To embed this into a markdown page, simply use a similar construct as the file answers, but with a base64 link instead: ![**description**](data:image/**type**;base64,**base64 data**). Now your image is 100% embedded into your Jupyter Notebook file!

Example link: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABHNCSVQICAgIfAhkiAAAAD9JREFUGJW1jzEOADAIAqHx/1+mE4ltNXEpI3eJQknCIGsiHSLJB+aO/06PxOo/x2wBgKR2jCeEy0rOO6MDdzYQJRcVkl1NggAAAABJRU5ErkJggg==

Example markdown: ![smile](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABHNCSVQICAgIfAhkiAAAAD9JREFUGJW1jzEOADAIAqHx/1+mE4ltNXEpI3eJQknCIGsiHSLJB+aO/06PxOo/x2wBgKR2jCeEy0rOO6MDdzYQJRcVkl1NggAAAABJRU5ErkJggg==)


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