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

后端 未结 15 1088
天命终不由人
天命终不由人 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:39

    In addition to the other answers using HTML (either in 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 need to overwrite the CSS height attribute instead:

    <img src="image.png" style="height:50px"> <-- works
    
    0 讨论(0)
  • 2020-11-30 16:41

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

    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.

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

    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 \

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

    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.

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

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