531

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
Reblochon Masque
37k10 gold badges60 silver badges85 bronze badges
asked Sep 3, 2015 at 8:09
1
  • Note that I drag images from my desktop to my notebook, and this works fine. Except I realize, one limitation is that you can only have one image per Markdown cell. Commented May 23, 2023 at 18:21

20 Answers 20

489

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
answered Sep 3, 2015 at 8:52
Sign up to request clarification or add additional context in comments.

24 Comments

For me, this doesn't work when downloading the notebook as html. It shows a broken image link. Using IPython.display.Image works as expected.
@cheflo The above syntax doesn't embed the image in the html file. If you look in the sourcecode of the html file, you will find an entry like <img src="img/picture.png" alt="title" > . To see the picture you have to copy it in the folder img relativ to the html file.
I suspected that was the case, thanks for clarifying. I thought this difference between the two approaches was unexpected and, at least for me, determined which one to choose, so I wanted to bring attention to it.
while doing it via markdown, ensure image file is in the same directory in which your notebook lies, for some strange reason (in my case), jupyter-notebook was not convinced to act on full path... ![title](picture.png)
@ArunKumarKhattri - had the same issue, only reletive path to the notebook worked - in my case: ![title](../img/picture.png)
|
333

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(url='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

prusswan
7,1514 gold badges44 silver badges65 bronze badges
answered Sep 3, 2015 at 8:23

12 Comments

It is true that, the possibility to resize the picture is interesting. But for future rendering, markdown is better for me.
can you put a local one with absolute path, via markdown ? i tried but it doesn't seem to work. Edit: Firefox (and Chrome) don't allow access to local files for security
@SwimBikeRun you should select the right cell type. your current cell type must be code
Underrated answer. Very thorough
the markdown version doesn't allow spaces in file names, for anyone running into that problem
|
111

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 />
Piotr Czapla
26.8k26 gold badges105 silver badges123 bronze badges
answered Apr 4, 2016 at 19:05

4 Comments

Great! I much prefer this answer since we have the control on the output size ! That's important for example when using the pelican tool to publish jupyter notebook as static html pages.
HTML doesn't need commas — just put a space between your attributes — and it's recommended to put quotes around all attribute values, e.g. width="60".
Yes it is awesome :) but ... the image is lost in PDF versions. Too bad.
this solution worked in Microsoft Edge, but does NOT work on Google Chrome. Unfortunately, Microsoft Edge keeps crashing, so having to go back to Chrome
68

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.

ejderuby
7356 silver badges23 bronze badges
answered Aug 23, 2019 at 19:59

4 Comments

Yet another helpful functionality added by the devs of Jupyter. One disadvantage to note, for cases when this matters: it would include the image in the IPyNB Jason code, thus making it much bigger and not as code revision friendly (e.g. when inspecting diffs).
Is there any capacity to change the dimensions or size of the picture this way? Otherwise, it's fairly large for me.
Markdown seems to be assuming a page width of 800 pixels or thereabouts. I rescaled my "large" image from 720 pixels wide to 360 pixels wide, and it appeared at half the size in the page.
This is the way. This allowed me to export my jupyter notebook as html and still see the image. Thank you!
43
  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

eldad-a
3,2313 gold badges26 silver badges26 bronze badges
answered Nov 4, 2019 at 13:41

4 Comments

Yes bat when I want download as HTML embebed , I get the error "nbconvert failed: missing attachment: imagen.png". ¿Why?
Pasting an image from the clipboard also works for me (after setting cell mode to Markdown)
I wonder if an embedded image can be used with HTML? That way we could specify the width and height.
as of JupyterLab version 3.6.8 "attachment:" needs to get removed to get the image display work, with markdown
33

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()
answered Jan 7, 2019 at 10:44

Comments

24

In addition to the other answers using HTML, either embedded into 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 to overwrite the CSS height attribute instead:

<img src="image.png" style="height:50px"> <-- works
answered Jan 16, 2018 at 20:16

Comments

21

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
answered Oct 22, 2016 at 16:19

Comments

16

Here's how you can do it with Markdown:

![Image of Yaktocat](https://octodex.github.com/images/yaktocat.png)
answered Sep 3, 2015 at 8:29

4 Comments

For some reasons, displaying images via markdown doesn't work for me... Are you sure the syntax is correct cristianmtr?
Yup, just tested it again. Are you sure you set the cell to Markdown? Also, what version of IPython/Jupyter are you using? Mine says: "The version of the notebook server is 4.0.4 and is running on: Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)]" Here's a screenshot with the results: i.imgur.com/4ISJFDE.png
beware with the paths, what works locally does not work online, .e. imgs\pic.png works locally, while online it is 1: case sensitve (Pic.png) 2: backslach and slash are not the same ... :/ –
No spaces are allowed in the filename for markdown, in case anyone else was running into this problem
13

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.

answered Jan 26, 2016 at 23:23

2 Comments

It seems option value was renamed to data in the meantime. For a locally stored image the code Image(data=open(filename_png, 'rb').read()) worked for me.
@jottbe I needed to still use value, as data didn't work with jupyter==1.0.0 notebook==6.4.0. So the solution is: Image(value=open(filename_png, 'rb').read())
12

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

answered Apr 15, 2020 at 0:13

1 Comment

This is exactly what I needed!
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:

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

Also watch out for / and \

answered Mar 27, 2016 at 22:24

1 Comment

This only works out for me. Thanks! "ShowMyImage" works like a charm!
6

I made some research on this topic and found 4 ways for inserting images in Jupyter Notebook that I've described in my article.

1. Drag-and-drop image to the cell (simplest!)

Please switch the cell to markdown type and just drag and drop the image there. The image will be internally encoded as Base64 and its text representation will be included in the ipynb file. I recommend this only for small images because your notebook ipynb file can become very large and laggy (if too many large images are dropped in).

2. Insert hosted image

Upload the image to the server (might be Imgur or GitHub) and enter the image URL in the Markdown:

![alternative text goes here](path-to-the-image)

3. Insert local file image

You can use the above method with a local image but it needs to have the same root directory as the notebook.

In the Markdown:

![alternative text](path-to-image)

or you can use Python:

from IPython import display
display.Image("path-to-image")
  1. Convert image to Base64 (only for small images)

Similar to the first step with a difference that conversion to Base64 is done manually.

The Python code:

from IPython import display
from base64 import b64decode
base64_data = "iVBORw0KGgoAAAANSUhEUgAABL ... the rest of data "
display.Image(b64decode(base64_data))

Should be used with rather small images.

I'm personally using the second method with images hosted in the GitHub repository.

answered Jun 9, 2022 at 13:29

Comments

4

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)
answered Dec 6, 2017 at 23:39

2 Comments

How does it differ from the Sebastian Stigler's answer?
Just wanted to highlight that it worked for me without including title within the squared bracket. Nothing different.
4

For some reason no other solutions work for me.(also works with namge.jpg).

Here what is working:

  1. Change cell to markdown
  2. Copy past:
<div>
<img src=attachment = "name.png" width="600"/>
</div> 
  1. Make an empty line after </div> in the same cell and drop your image using mouse into this line.
  2. You will see something like that:
<div>
<img src="attachment:name.png" width="600"/>
</div>
![yourname.jpg](attachment:yourname.jpg)
  1. Copy yourname.jpg and past it instead of name.png
  2. Remove ![yourname.jpg](attachment:yourname.jpg).
  3. The final result should look like:
<div>
<img src="yourname.jpg" width="600"/>
</div>
  1. Run sell (shift+enter) and you will see your image
answered Jan 11, 2022 at 21:49

Comments

3

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.

answered Mar 9, 2019 at 3:32

Comments

1

I'm assuming you mean in a markdown cell, inside a jupyter notebook. If so, here are the easiest ways for both:

From the local computer:

With your file browser, navigate to the directory containing your picture. Then, drag and drop it into the cell you want to embed the picture. It must already be in markdown mode or nothing will happen.

The pros of doing it this way are:

  • It's simple, just drag and drop

The cons of doing it this way:

  • You cannot copy and paste that link, and use it in any other cell. If you want to use the same picture in any other notebook cell, you will need to find the picture, drag and drop into the cell again.

  • You should consider a file management scheme that will allow you to find a picture again, if you plan on using allot of pictures in your notebooks.

This is an effective way of doing things that's worked for me.

From the web:

The easiest way is to simply find a picture using your web browser, right click and "Copy image", paste this into the cell you want the image displayed.

As above this does come with caveats in that you can't copy this and link and paste it into another cell. So if you plan on using the image again, you'll have to either paste into another cell, or save this image locally.

The images are embedded in the notebook so should work anywhere the notebook is opened.

answered Sep 6, 2021 at 14:51

Comments

1

IPython::display::display() can be used to display a PIL Image in a Jupyter notebook:

from PIL import Image
from IPython.display import display
img = Image.open('frog.png')
display(img)

This is the solution I came to after PIL::Image#show() failed in my Jupyter notebook:

# failure example
from PIL import Image
img = Image.open(master_img_path)
img.show()
# Notebook presents error:
# Error: no "view" rule for type "image/png" passed its test case
# (for more information, add "--debug=1" on the command line)
answered Apr 30, 2024 at 22:39

Comments

0

Insert images that are present locally

Quick Solution : Watch this Video (1:36) https://www.youtube.com/watch?v=Vw1EZh1My8s


In short :

  1. Go to the location where your .ipynb is present.
  2. Create folder called images and paste image that you want insert in jupyter in that images folder
  3. Open .ipynb file on browser and there click on cell and type :
  4. There is a dropdown button which is mentioned as Code. Change that to Markdown
  5. Hurray!! Now run the code by hitting shift + Enter

Reference image

enter image description here

answered Jan 25, 2022 at 16:47

Comments

-1

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

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"/>*
answered Feb 1, 2020 at 19:32

1 Comment

These are just variations of the path to the file on your computer, you might want to learn about absolute and relative paths.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.