Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I open images in a Google Colaboratory notebook cell from uploaded png files?

I am working with a Google Colaboratory notebook. I uploaded a file named bp.png into the working directory, and I can see that the file is in there by running !ls in a code cell. Then I try this piece of code to see the image in a markdown cell:

<h2 align="center">Image</h2> <img src="bp.png" width="600"> 

But the Colab notebook's cell stays empty after running that (except for the header), although if I run this in a local Jupyter notebook the image does appear in the cell in that local notebook.

UPDATE:

I know I can use files uploaded to the working directory because my custom .py files that I upload, get imported to my Colab notebooks without any problems. For example, I can upload a file py_file.py and then in the Colab notebook use it as in from py_file import some_function, and it works.

like image 770
Sergey Zakharov Avatar asked Mar 25 '18 17:03

Sergey Zakharov


People also ask

How do I display an uploaded image in Colab?

Select the uploaded image, right-click on it, get a sharable link & copy it. Open Google Colab Notebook & add text block where you want to include the image. The general code to include an image is given below. Paste the sharable-link of your image that you had copied earlier inside round brackets.

How do I add an image to a Google colab notebook?

Right-click your image and you will find an option to get a sharable link. On selecting 'Get shareable link', Google will create and display sharable link for the particular image. The format may change in the future but all we need to embed our image is the <ID of image> from URL.

How to add images to Google Colab notebook?

Step I: Upload the image on google drive & copy the sharable link. First, open google drive & upload the image on the drive. Select t h e uploaded image, right-click on it, get a sharable link & copy it. Open Google Colab Notebook & add text block where you want to include the image.

How do I upload files to Google Colab?

You can upload files manually to you google colab working directory by clicking on the folder drawing button on the left. They are then accessible just as they would be on your computer. Show activity on this post. The simplest way to upload, read and view an image file on google Colab.

Can I access GitHub from a Google Colab notebook?

Since a Colab notebook is hosted on Google’s cloud servers, there’s no direct access to files on your local drive (unlike a notebook hosted on your machine) or any other environment by default. However, Colab provides various options to connect to almost any data source you can imagine. Let us see how. Accessing GitHub from Google Colab

Can I read and write to Google Drive from Colab?

Now you can interact with your Google Drive as if it was a folder in your Colab environment. Any changes to this folder will reflect directly in your Google Drive. You can read the files in your Google Drive as any other file. You can even write directly to Google Drive from Colab using the usual file/directory operations.


2 Answers

Try this

from IPython.display import Image Image('bp.png') 

You can set width and height as well

Image("bp.png", width=100, height=100) 

To display more than 1 image, you need to call display. (it’s auto for just 1 image)

from IPython.display import Image, display display(Image('1.png')) display(Image('2.png')) 

Update jan/2019

Put your image in /usr/local/share/jupyter/nbextensions/

Then display it from /nbextensions/, e.g.

%%html <img src='/nbextensions/image.png' /> 

Update feb/2022

In Google Colab, open the file browser icon (left nav bar) and navigate to usr/local/share/jupyter/nbextensions as described above. Click the ellipsis menu on the nbextensions folder > Upload and select your image to upload. Make sure to update the img tag from the code snippet above with the correct file name, and you'll want to use a code box (not text). Note that your image will be deleted from the directory when your runtime is recycled (I got a warning dialog stating so when I uploaded my image), so be careful with that.

like image 172
korakot Avatar answered Nov 10 '22 21:11

korakot


One can also display images in the markdown/Text cell in Colab. Create a Text cell and then you will have a top bar with icons. Select the image icon corresponding to "Insert images" and then choose from you local machine the image. It seems like it doesn't let you select from the google drive, though

enter image description here

like image 21
NeStack Avatar answered Nov 10 '22 22:11

NeStack