10
votes

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.

3

3 Answers

12
votes

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.

6
votes

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)
6
votes

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.