Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 221
Ger Avatar asked Sep 03 '15 08:09

Ger


People also ask

How do I use local files in Jupyter Notebook?

Right-click on a file or directory and select “Copy Shareable Link” to copy a URL that can be used to open JupyterLab with that file or directory open. Right-click on a file or directory and select “Copy Path” to copy the filesystem relative path.

How does Jupyter Notebook read pictures from a folder?

To read the image using OpenCV I have defined load_images_from_folder function which takes a path where images are stored as an input parameter , In the next step cv2. imread function read all files in a folder and append them to images =[] list then return images list.


2 Answers

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 
like image 97
Sebastian Stigler Avatar answered Oct 04 '22 15:10

Sebastian Stigler


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

like image 22
Reblochon Masque Avatar answered Oct 04 '22 15:10

Reblochon Masque