11

I am running Python 3.7 on Windows using pycharm. I have a jupyter notebook and I would like to embed an image into the notebook. I know all the ways of doing standard embedding with markdown language, BUT ideally what I want is:

a. Embed the image via markdown language, i.e. the notebook cell is in 'markdown' state, not 'Code' state, AND ALSO

b. Have it able to export to HTML and retain that image in the HTML file. i.e. on the notebook. I want to click File -> Download as -> HTML (.html), save the notebook file in .html format, and then when I send it to my buddy, the image that I attached is in the notebook, and he sees it.

I know i could do this in a cell ('code'):

from IPython.display import Image
Image(filename="myfile.jpg")

but I do not want to use 'Code', since when I send to my buddy, he will see the In [] code statement and the Out [] of the image in the notebook html file.

Note: This would be an image that was on my laptop that I would want in the html formatted exported notebook. It is NOT on the web where he could refer to it with a www type statement. Unless I'm crazy, there is no way to do this with markdown command in a cell, the only way to do it (with the image embedded 'permanently' into the .html format of the notebook), would be via a cell that was in 'Code' celltype.

m00am
  • 5,910
  • 11
  • 53
  • 69
TexasTom
  • 309
  • 1
  • 2
  • 6

4 Answers4

15

When you use a code cell to show an image and then export the notebook to an HTML file, the image is converted to Base64 and the code directly used in the src attribute of the <img> tag. You can apply the same procedure with images included in markdown cells.

  1. First, encode your image as Base64, e.g. by using one of the online enocders.
  2. Create a markdown cell and include an <img> tag which uses your Base64 code, e.g.:

    <img src="data:image/png;base64,CODE_FOLLOWS_HERE" />
    
  3. Evaluate the cell and you should already see your image.

If you now export your notebook to HTML, the image should be included in the file the same way as images from code cells.

The only disadvantage with this approach is that your markdown cell gets cluttered with the (probably long) Base64 code. However, this is manageable by e.g. using a markdown cell dedicated solely to the image without other content.

Milania
  • 565
  • 7
  • 15
  • 1
    Plus, you can add something like ` style="max-width:150%; width: 150%" ` between the final quote symbol and before `/>` to get the image to display larger than the current default cell area and with scroll bars, if necessary. (Based on [here](https://github.com/holoviz/holoviews/issues/79#issuecomment-102642216).) – Wayne Dec 20 '19 at 20:25
12

You can install the Unofficial Jupyter Notebook Extensions.

It has some interesting extensions (e.g. spell checker, collapsible headings, ...). One of the extensions is Export HTML With Embedded Images which exactly does what you want.

To install Nbextensions using pip do the following:

$ pip install jupyter_contrib_nbextensions
$ pip install jupyter_nbextensions_configurator
$ jupyter contrib nbextension install --user 
$ jupyter nbextensions_configurator enable --user

Then you will see in your Jupyter homepage a new tab (Nbextensions), where you can enable and configure different extension.

After enabling the "Export HTML With Embedded Images", you will see the corresponding option in the "File-Download as" menu.

zardosht
  • 3,014
  • 2
  • 24
  • 32
8

My complete solution is based on Milania and

the code

    import base64, io, IPython
    from PIL import Image as PILImage

    image = PILImage.open(image_path)

    output = io.BytesIO()
    image.save(output, format='PNG')
    encoded_string = base64.b64encode(output.getvalue()).decode()

    html = '<img src="data:image/png;base64,{}"/>'.format(encoded_string)
    IPython.display.HTML(html)
venergiac
  • 7,469
  • 2
  • 48
  • 70
0

For me, on Visual Studio Code, something like this did the trick (in a markdown cell, as you requested, and a image that you want to embed in your notebook and further be exported to the html output):

<figure>
<img src="./notebook_img/diptera_taxat_yes_no.jpg" width="200"/>
<figcaption>Limit the search on Diptera</figcaption>
</figure>

Where the image is located in "./notebook_img" relative to the location of the notebook (in this sense, the notebook is located in .)

Your buddy will not see the code from above when reading the HTML exported file, so that should satisfy the requested need as far as I understand. He will also not need the folder "notebook_img".

Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68