5

How can I show the pictures from folder figures in google colab inline in a markdown cell?

I have the following structure of notebooks on my local drive.

figures
    - pic1.png
    - pic2.png
    - ...
Notebook1.ipynb
Notebook2.ipynb
etc.

After opening a Notebook in google colab and uploading the figures folder, I checked that the folder with pictures is actually uploaded. It is.

I then tried the following in a markdown cell:

![Pic1](figures/pic1.png)

This apparently doesn't work in google colab.

How did Jake VanderPlas do this here? Apparently he didn't even upload the pictures into a folder "figures" but still uses the following line in markdown

![Broadcasting Visual](figures/02.05-broadcasting.png) in the above link (scroll down to see a picture on numpy arrays).

Any help is appreciated!

Thanks!

FredMaster
  • 1,211
  • 1
  • 15
  • 35

3 Answers3

4

GitHib image references are resolved relative to the repo.

For notebooks stored in Drive, you'll need to embed the image in the notebook. Here's an example:

enter image description here

https://colab.research.google.com/drive/1jWHKR6rhhyZtUulttBD6Pxd_AJhgtVaV

The key bit is the Image display helper, applied to a local file–

from IPython.display import Image
Image('220px-TensorFlowLogo.svg.png')
Bob Smith
  • 36,107
  • 11
  • 98
  • 91
2

Here is another way to show it.

#@markdown Fig.1. Pic1 demo

import matplotlib.pyplot as plt

pic_name = '/content/drive/MyDrive/pic1.png'
image=plt.imread(pic_name)

fig=plt.figure(figsize=(20,30))
_=plt.imshow(image)
_=plt.axis('off')

Markdown tag shows text, which allows code hiding from the report (if that is your end goal).

Nikolai Zaitsev
  • 303
  • 5
  • 9
1

Once you upload your local image to your Google Colab filesystem (click the folder icon on the upper left of your notebook that says "files", then upload it), you can use the IPython code to display it. You get the path link by right-clicking on the image after you uploaded it that says "copy path". Put the whole link in quotes.

from IPython import display
display.Image("/content/my-image.png")
Azurespot
  • 3,066
  • 3
  • 45
  • 73