325

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?

lczapski
  • 4,026
  • 3
  • 16
  • 32
benpro
  • 4,325
  • 4
  • 19
  • 17

14 Answers14

361

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

Philipp Schwarz
  • 18,050
  • 5
  • 32
  • 36
  • 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 Dec 22 '16 at 18:04
  • 3
    @ 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 : https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet – Philipp Schwarz Jan 28 '17 at 16:32
  • 1
    @PhilippSchwarz are there some options to your example above to control the size of the 'pasted' image? Using the `` tag, such options appear approximately like this ``. – Reb.Cabin Jun 30 '17 at 14:57
  • 2
    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). – Mark Sep 17 '17 at 12:55
  • 2
    Is there any way to use this method with image files that have space in their names? – zebralamy Dec 30 '18 at 07:34
  • Also looking for a way to deal with files that has space in the name/path directories. – user8491363 Mar 21 '20 at 18:13
  • it needs to be encoded like it is done for url. use %20 for spaces in filenames – munish Dec 24 '21 at 14:44
269

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.

Pierre H.
  • 388
  • 1
  • 11
minrk
  • 37,545
  • 9
  • 92
  • 87
  • What is the "notebook directory"? Are you saying that the image should be in the same directory as the *.ipynb file? I tried this, but it didn't work. – benpro May 17 '12 at 01:29
  • 3
    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). – minrk May 17 '12 at 04:30
  • 137
    Since you're in markdown, why not use `![caption](files/image.png)`? – kynan Mar 16 '13 at 00:09
  • @kynan - there is no reason not to, unless you are adding shape info, etc. that markdown doesn't support. – minrk Mar 16 '13 at 04:11
  • 4
    @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. – Abid Rahman K Apr 24 '13 at 04:40
  • 4
    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"`. – minrk Apr 24 '13 at 05:04
  • 1
    Both yours and @kynan works really well. But they don't update if I change the image in the notebook dir – Norfeldt Jul 23 '13 at 10:55
  • 2
    you have to refresh a webpage for images at a given url to be reloaded. – minrk Jul 23 '13 at 15:41
  • 2
    It worked for me :). Thanks. Just a reminder that markdown code works as well: ![some text](files/image.png) works perfect too! – Alejandro Sep 04 '13 at 00:39
  • Where is the notebook directory? – Hamman Samuel Mar 17 '15 at 00:23
  • 1
    How can I resize the image? – an offer can't refuse May 17 '15 at 07:41
  • As old as this answer is, it's still the most reliable in my experience. I have run into strange behavior using the other methods (e.g. if the name of image start with a U or an x using the coding method, etc.. I can guess what's going here, but still annoying) – Michael Szczepaniak Jan 11 '18 at 23:28
  • 1
    This does not embed images in your notebook. What I mean by that is when you publish your notebook on GitHub for instance, the images are lost and the tags are displayed as text instead. This is unlike the Image method from IPython.display mentioned in the other answer. – RodrikTheReader Jul 11 '18 at 13:51
97

I am using ipython 2.0, so just two line.

from IPython.display import Image
Image(filename='output1.png')
Gomes
  • 3,330
  • 25
  • 17
  • 33
    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. – Mike Feb 09 '15 at 17:32
  • 3
    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'))` – Matteo T. Aug 02 '16 at 21:24
66

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)

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

Rich Lysakowski PhD
  • 2,702
  • 31
  • 44
  • 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? – Rich Lysakowski PhD Mar 24 '20 at 04:33
  • 2
    I am even drag-dropping from local machine to my remote notebook. Thank you – BND Sep 22 '20 at 17:27
  • 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==" } }, – Steve L Oct 28 '20 at 14:07
  • 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. – Rich Lysakowski PhD Mar 19 '21 at 05:04
  • 1
    I am using Jupyter Notebook 6.4.4 with Python 3.8.9 Under Windows 7 64bit Sp1, and it works fine. – 24b4Jeff Nov 24 '21 at 15:06
39

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)
seralouk
  • 30,938
  • 9
  • 118
  • 133
27

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

Mike
  • 2,637
  • 5
  • 29
  • 40
15

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')
approxiblue
  • 6,982
  • 16
  • 51
  • 59
hydroman
  • 158
  • 1
  • 5
  • 1
    documentation reference http://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html#IPython.display.Image , although using markdown ( `![alt text](foo.png "the title")` is imho preferable, unless using additional args from api is required. – michael Aug 21 '17 at 00:29
15

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!

tsando
  • 4,557
  • 2
  • 33
  • 35
  • 1
    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. – insanely_sin Nov 01 '18 at 06:38
11

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)
Jade Cacho
  • 691
  • 6
  • 9
10

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

Romain Jouin
  • 4,448
  • 3
  • 49
  • 79
  • 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. – Tulio Casagrande May 21 '19 at 21:21
4

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)
Sergey Shcherbakov
  • 4,534
  • 4
  • 40
  • 65
3

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.

0

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

-4

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