问题
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 :

I also tried

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
回答1:
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

to

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
回答2:
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)
for a web image:

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

demonstrated by @Sebastian
回答3:
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>
回答4:
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:
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:
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()
回答7:
Here's how you can do it with Markdown:

回答8:
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.
回答9:
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:

Also watch out for / and \
回答10:
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:
Convert the cell to
markdownby:- 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)
- pressing M on the selected cell
Now go to menu bar and select Edit -> Insert Image.
Select image from your disk and upload.
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.
回答11:
This works for me in a markdown cell. Somehow I do not need to mention specifically if its an image or a simple file.

回答12:
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:
- Set cell mode to Markdown
- Drag and drop your image into the cell. The following command will be created:

- 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.
来源:https://stackoverflow.com/questions/32370281/how-to-embed-image-or-picture-in-jupyter-notebook-either-from-a-local-machine-o