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

后端 未结 15 1090
天命终不由人
天命终不由人 2020-11-30 16:12

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\")
         


        
相关标签:
15条回答
  • 2020-11-30 16:49

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

    0 讨论(0)
  • 2020-11-30 16:53

    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()
    
    0 讨论(0)
  • 2020-11-30 16:53
    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

    0 讨论(0)
  • 2020-11-30 16:56

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

    0 讨论(0)
  • 2020-11-30 16:56

    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 />
    
    0 讨论(0)
  • 2020-11-30 16:58

    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
    
    0 讨论(0)
提交回复
热议问题