How to embed image or picture in jupyter notebook, either from a local machine or from a web resource?

632,013

Solution 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

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

Solution 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(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

Solution 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 />

Solution 4

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.

Solution 5

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()
Share:
632,013
Ger
Author by

Ger

I am associate professor in numerical simulations and physical-chemistry at Pau (France) in Université de Pau et des Pays de l'Adour. I did my phd in Paris XI (Orsay) on the study, by numeric simulations, of the photophysics properties of Fluorescent Proteins.

Updated on January 26, 2022

Comments

  • Ger
    Ger over 2 years

    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
    Reblochon Masque over 8 years
    For some reasons, displaying images via markdown doesn't work for me... Are you sure the syntax is correct cristianmtr?
  • cristianmtr
    cristianmtr over 8 years
    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
  • Ger
    Ger over 8 years
    It is true that, the possibility to resize the picture is interesting. But for future rendering, markdown is better for me.
  • joelostblom
    joelostblom over 8 years
    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.
  • Sebastian Stigler
    Sebastian Stigler about 8 years
    @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.
  • joelostblom
    joelostblom about 8 years
    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.
  • Arun Kumar Khattri
    Arun Kumar Khattri almost 8 years
    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)
  • Svend
    Svend over 7 years
    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.
  • Jeremy
    Jeremy over 7 years
    When I do either <img src="pyplot_annotation.png" alt="Annotation Examples" style="width:600px"> or ![Annotation Examples](pyplot_annotation.png) all I get is Annotation Examples and no image. The pyplot_annotation.png is in the same directory as the ipynb file.
  • Jeremy
    Jeremy over 7 years
    I found an answer to my problem: stackoverflow.com/questions/40527191/…
  • ohad edelstain
    ohad edelstain almost 7 years
    @ArunKumarKhattri - had the same issue, only reletive path to the notebook worked - in my case: ![title](../img/picture.png)
  • Ciprian Tomoiagă
    Ciprian Tomoiagă over 6 years
    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
  • colllin
    colllin about 6 years
    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".
  • WestCoastProjects
    WestCoastProjects about 6 years
    This is not working as is. More investigations into relative paths are needed for this to be a robust answer.
  • WestCoastProjects
    WestCoastProjects about 6 years
    @CiprianTomoiagă What do you mean ? chrome/ff can display local files
  • Ciprian Tomoiagă
    Ciprian Tomoiagă about 6 years
    I meant that if the path is not under the tree where jupyter was started, you cannot show it. e.g. !tmp_img . I think I was getting some permission warning in chrome console in some cases
  • Sebastian Stigler
    Sebastian Stigler about 6 years
    @javadba can you specify your problem?
  • SwimBikeRun
    SwimBikeRun about 6 years
    Why doesn't this work. When doing !Image of Yaktocat, I get /bin/sh: 1: Syntax error: "(" unexpected
  • Reblochon Masque
    Reblochon Masque about 6 years
    Can you please be more specific as to exactly what you were not able to make work?
  • Sergey Shcherbakov
    Sergey Shcherbakov almost 6 years
    The problem is the local path on the Jupyter machine where to place files so that they could be accessible in the demonstrated way?
  • Sebastian Stigler
    Sebastian Stigler almost 6 years
    @SergeyShcherbakov If you run the jupyter notebook command in the path where your notebook is, you have to put the image in a subdirectory of this path with the name img.
  • Sergey Shcherbakov
    Sergey Shcherbakov almost 6 years
    Thank you @SebastianStigler, that worked. Would be nice to add this detail to your answer.
  • youkaichao
    youkaichao almost 6 years
    @SwimBikeRun you should select the right cell type. your current cell type must be code
  • M.Innat
    M.Innat over 5 years
    How does it differ from the Sebastian Stigler's answer?
  • Trect
    Trect over 5 years
    Holy sh*t. This is super awesome
  • Pranav Pandit
    Pranav Pandit over 5 years
    Just wanted to highlight that it worked for me without including title within the squared bracket. Nothing different.
  • zebralamy
    zebralamy over 5 years
    It's been long but what if my file name contains a blank? like test 1.png?
  • Reblochon Masque
    Reblochon Masque over 5 years
    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 - on windows, I have no idea.
  • Intelligent-Infrastructure
    Intelligent-Infrastructure about 5 years
    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 ... :/
  • Intelligent-Infrastructure
    Intelligent-Infrastructure about 5 years
    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 ... :/ –
  • Kareem Jeiroudi
    Kareem Jeiroudi about 5 years
    What Arun Kumar Khattri said is important! Jupyter Notebook doesn't take full paths into account. So it has to be either in the same directory as the kernel, or in a subdirectory. However, I'd definitely advise you to use html syntax, because then you can specify more attributes such as the dimensions of the image.
  • Nathan
    Nathan almost 5 years
    Underrated answer. Very thorough
  • Nathan
    Nathan almost 5 years
    Please see the 2nd answer by @Reblochon Masque too; it's more thorough
  • jottbe
    jottbe over 4 years
    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.
  • Pranzell
    Pranzell over 4 years
    This only works out for me. Thanks! "ShowMyImage" works like a charm!
  • Mohammad Heydari
    Mohammad Heydari over 4 years
    Saved me!, Thanks
  • efueyo
    efueyo about 4 years
    Yes bat when I want download as HTML embebed , I get the error "nbconvert failed: missing attachment: imagen.png". ¿Why?
  • james-see
    james-see about 4 years
    Works great in Python 3.8. Thanks
  • CoffeeTableEspresso
    CoffeeTableEspresso about 4 years
    the markdown version doesn't allow spaces in file names, for anyone running into that problem
  • CoffeeTableEspresso
    CoffeeTableEspresso about 4 years
    No spaces are allowed in the filename for markdown, in case anyone else was running into this problem
  • CgodLEY
    CgodLEY about 4 years
    Pasting an image from the clipboard also works for me (after setting cell mode to Markdown)
  • Sebastian Stigler
    Sebastian Stigler about 4 years
    @CoffeeTableEspresso of course it can. You have to html escape the spaces in the name. If the file is called The picture with spaces.jpg then you have to type the following in the notebook: ![alt text for the picture](The%20picture%20with%20spaces.jpg)
  • CoffeeTableEspresso
    CoffeeTableEspresso about 4 years
    @SebastianStigler I did not realize you could do that, thanks for that
  • eldad-a
    eldad-a about 4 years
    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).
  • babou
    babou almost 4 years
    Yes it is awesome :) but ... the image is lost in PDF versions. Too bad.
  • WorldLover
    WorldLover over 3 years
    Awesome! That works well! Thank you so much!
  • ah bon
    ah bon about 3 years
    It raises error for my case: /bin/sh: -c: line 0: syntax error near unexpected token img/picture.jpg'; /bin/sh: -c: line 0: [title](img/picture.jpg)'
  • Brian Mark Anderson
    Brian Mark Anderson almost 3 years
    Make sure that you change img/picture.png to img/picture.PNG if you want the images you've added to render properly in Github!
  • Bartłomiej Skwira
    Bartłomiej Skwira almost 3 years
    This did NOT work for me in Jupyter Lab 3.0.7. When I do ![title](img/picture.png) I get /bin/sh: 1: Syntax error: word unexpected (expecting ")")
  • Sebastian Stigler
    Sebastian Stigler almost 3 years
    @BartekSkwira This error message shows that you executed this string in a python cell and not a markdown cell. Change the cell type to markdown and it will work.
  • Karol Zlot
    Karol Zlot almost 3 years
    @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())
  • Bartłomiej Skwira
    Bartłomiej Skwira almost 3 years
    @SebastianStigler omg You're right :) I didn't change cell type to Markdown, now it worked perfectly. Thanks!
  • cjmaria
    cjmaria over 2 years
    These are just variations of the path to the file on your computer, you might want to learn about absolute and relative paths.
  • Tahmid Rafi
    Tahmid Rafi over 2 years
    This is exactly what I needed!
  • Hofbr
    Hofbr over 2 years
    Is there any capacity to change the dimensions or size of the picture this way? Otherwise, it's fairly large for me.
  • Andy
    Andy over 2 years
    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.
  • Kai
    Kai about 2 years
    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
  • James Hirschorn
    James Hirschorn about 2 years
    I wonder if an embedded image can be used with HTML? That way we could specify the width and height.