A Seamless Guide to Embedding Photos into Google Colab

Liu Na
2 min readFeb 19, 2024

--

A picture is worth a thousand words, especially in the interpretation and presentation of data. Google Colab, a popular platform, enables analysts to share, write, and execute code in an interactive environment. One common requirement is the inclusion of images directly within notebooks, which can enhance understanding or provide necessary visual context. However, directly inserting images, especially from a source like Google Drive, and controlling their appearance can seem daunting. This article demystifies the process, providing a clear, step-by-step guide on how to seamlessly integrate photos from Google Drive into your Google Colab notebooks.

Step 1: Upload the Image to Google Drive

First, upload the image to your Google Drive: click the “+New” button on the upper left corner under “Drive” icon, then choose “file upload”

Step 2: Make the Image Shareable

  • Right-click on the uploaded image in Google Drive.
  • Select “Share”.
  • Click on “Change to anyone with the link” to make the file publicly accessible.
  • Copy the shareable link provided by Google Drive.

Step 3: Convert the Shareable Link to an Embeddable Link

The shareable link will look something like this:

https://drive.google.com/file/d/FILE_ID/view?usp=sharing

You need to convert this to an embeddable link format:

https://drive.google.com/uc?export=view&id=FILE_ID

Replace `FILE_ID` with the actual ID of your file from the shareable link.

Step 4: Insert the Image with HTML in a Markdown Cell

To insert the image, you can use the following markdown:

![Image description](https://drive.google.com/uc?export=view&id=FILE_ID)To insert the image and specify its size, use the following HTML code in a Markdown cell in your Colab notebook:

Replace Image description with a brief description of your image, and FILE_ID with the actual file ID from your link.

Alternatively, to insert the image and specify its size, use the following HTML code in a Markdown cell in your Colab notebook:

<img src=”https://drive.google.com/uc?export=view&id=YOUR_FILE_ID” width=”WIDTH” height=”HEIGHT” />

Replace YOUR_FILE_ID with your actual file ID, and WIDTH and HEIGHT with the desired dimensions of the image in pixels.

Example:

If your image id is 1234abcd and you want the image to be 500 pixels wide and 250 pixels tall, the HTML code would be:

<img src=”https://drive.google.com/uc?export=view&id=1234abcd” width="500" height="250" />

Note:

Making your image publicly accessible means anyone with the link can view it. Consider the privacy of your content before sharing.

Specifying only one of either width or height will scale the image proportionally, maintaining its aspect ratio.

Make sure to replace the placeholders like `YOUR_FILE_ID`, `WIDTH`, and `HEIGHT` with the actual file ID and dimensions you wish to use.

By integrating visual elements, you’ll not only enrich your presentations but also foster a deeper understanding and appreciation of your work among your audience. So, go ahead, give it a try, and witness the transformative impact on your work’s clarity and visual appeal.

--

--