之类的内容添加到降价中,但据我所知,这就是我所知道的。我假设我可以将图像放在由 127.0.0.1:8888 (或某个子目录)表......" /> 之类的内容添加到降价中,但据我所知,这就是我所知道的。我假设我可以将图像放在由 127.0.0.1:8888 (或某个子目录)表......"> 之类的内容添加到降价中,但据我所知,这就是我所知道的。我假设我可以将图像放在由 127.0.0.1:8888 (或某个子目录)表......" />
ChatGPT解决这个技术问题 Extra ChatGPT

Inserting image into IPython notebook markdown

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?


P
Philipp Schwarz

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).


Curious as to how you know this command. Is it unique to Jupyter or is this some language (latex formatting, perhaps) that you are using within Jupyter to add the image?
@ Alex G, Markdown is alanguage with plain text formatting syntax designed so that it can be converted to HTML and many other formats. Markdown is often used to create rich text using a plain text editor. Check out the following link to learn the syntax : github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
@PhilippSchwarz are there some options to your example above to control the size of the 'pasted' image? Using the <img...> tag, such options appear approximately like this <img ... width="480">.
Using this it did not work. Notebook was looking in a specific location /notebooks/ - surely this isn't root - and local path did not exist. I found it helpful to just use the notebook insert image - which added the image as an attachment (which is now portable).
Is there any way to use this method with image files that have space in their names?
P
Pierre H.

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.


Ah, you are probably using 0.12, aren't you? Serving local files is currently only available in master. And yes, the "notebook directory" is the directory with the notebooks (.ipynb files).
Since you're in markdown, why not use ![caption](files/image.png)?
@minrk : it doesn't work for me. I put " ", but it shows nothing. k.png is in the same folder as that of notebook file.
Please re-read the answer and comments. Your url will always start with 'files/', so if you have k.png next to your notebook, the URL would be src="files/k.png".
you have to refresh a webpage for images at a given url to be reloaded.
G
Gomes

I am using ipython 2.0, so just two line.

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

This is the correct way to display an image in a code cell. minrk's answer is the correct way to display an image in a markdown cell.
Using jupyter notebook 4.2.0, it doesn't show up the image unless I call display too: from IPython.display import Image, display; display(Image(filename='output1.png'))
R
Rich Lysakowski PhD

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

Simply create an empty Markdown cell. 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)

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 !!

UPDATE AS OF March 18, 2021: This simple "Drag-and-Drop-from-Windows-File-System" method still works fine in JupyterLab. JupyterLab inserts the proper HTML code to embed the image directly and permanently into the notebook so the image is stored in the .ipynb file. I am running Jupyter Lab v2.2.7 on Windows 10 Python 3.7.9 still works in JupyterLab. I am running Jupyter Lab v2.2.7 using Python 3.7.9 on Windows 10.

This stopped working in Jupyter Classic Notebook v6.1.5 sometime last year. I reported an bug notice to the Jupyter Classic Notebook developers.

It works again in the latest version of Jupyter Classic Notebook. I just tried it in v6.4 on 7/15/2021. Thank you Jupyter NB Classic Developers !!


Unfortunately this method doesn't work when Jupyter Notebook version 6.0 is hosted in Google Cloud. Anyone have a workaround that works without Admin privileges, something besides installing the drag-and-drop nbextension?
I am even drag-dropping from local machine to my remote notebook. Thank you
While I wasn't (like BND) able to drag a figure from my local machine into a remotely hosted notebook, I was able to do that when I was hosting locally, and moreover I confirmed that the actual image is embedded within the notebook file so you can then send the .ipynb file to others and the graphic will be preserved. The markdown cell with the graphic looks something like this in a plain text editor (image data shortened ...): { "attachments": { "download.png": { "image/png": "iVBORw0KGgoAAAAN ... U5ErkJggg==" } },
This simple "Drag-and-Drop-from-Windows-File-System" method still works fine in JupyterLab. A bug was reported for Jupyter Classic Notebook v6.x.
I am using Jupyter Notebook 6.4.4 with Python 3.8.9 Under Windows 7 64bit Sp1, and it works fine.
s
seralouk

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

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

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)

In the markdown cell, for me, works without the commas: <img src="files/image.png" width=800 height=400>
it is true that commas are not needed in HTML commands. see my update
This is a better solution if you want to control the size of the displayed image in your Jupyter notebook. Otherwise it can work but be a horror story.
M
Mike

[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.


This answer needs some upvotes. Very few know about Ipython extensions.
just tried drag'n'drop in Safari and Chrome, not working for me (OSX, Py3, all updates)
The second link you posted is broken -- extension no longer supported?
I also can't find the extension anymore.
Yes, Jupyter has moved on a bit in the last three years and this answer is quite obsolete.
a
approxiblue

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')

documentation reference ipython.readthedocs.io/en/stable/api/generated/… , although using markdown ( ![alt text](foo.png "the title") is imho preferable, unless using additional args from api is required.
t
tsando

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!


For me, images with white spaces in the name were not being rendered in the notebook. I removed the spaces, I am able to see now.
J
Jade Cacho

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)

R
Romain Jouin

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


I prefer this option, since it embeds the image in the notebook binary. It makes the notebook heavier, but you don't need to keep the image in the relative path.
S
Sergey Shcherbakov

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)

j
jlarchibald

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.


M
Michael Thompson

I never could get "insert image" into a markdown cell to work. However, the drag and drop entered the png file saved in the same directory as my notebook. It brought this text into the cell

""

The shift + enter > image is now displayed in notebook.

FWIW


N
Noman Nazir

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