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
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
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
There are several ways to post an image in Jupyter notebooks:
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
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)
- 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:
demonstrated by @Sebastian
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 />
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.
Convert the cell to
- pressing M on the selected cell
- 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.
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()
I’m surprised no one here has mentioned the html cell magic option.
from the docs (IPython, but same for Jupyter)
Render the cell as a block of HTML
In addition to the other answers using HTML (either in Markdown or using the
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
- 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.
Edit: Works in JupyterLab Version 1.2.6