bbbb
bbbb

Reputation: 341

How to display an image within the notebook in Google Colab (like in Anacondan Jupyter Notebook)?

I am writing a Google Colab Notebook about Tight Binding Theory. I want to display either in a markdown cell or in a code cell an image, like its possible to do in Anaconda with the following code

from IPython.display import Image # needed to embed an image
Image(filename='example.png', embed=True)

I have tried doing it this way in Google Colab:

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

It runs and nothing shows up.

I have read that this way is also possible:

  1. putting your image in /usr/local/share/jupyter/nbextensions/google.colab/

  2. <img src='/nbextensions/google.colab/image.png' /> ```
    

I don't really understand this last way of doing it. Like is that first step a directory in my computer (I have tried looking for it and it is not there)?

Is there a simpler way to approach this?

EDIT: I realise now that directory is for LINUX operating systems. Any way I could do something equivalent in Windows (My computer operates Windows)

Upvotes: 1

Views: 11321

Answers (3)

Sukhen Dey
Sukhen Dey

Reputation: 35

Google Colab, in my opinion, is probably the most connected platform for Python and Data Science related programming. It is an infrastructure issue. Google dominates over 95% of the Internet search activities and Colab is connected with Google Data Studio for Big data Analytics, Google Search Console, and all repositories of data and algorithms from Google. Thus, you can pull the entire internet from Colab, so to speak. You are thinking “Cut to the chase.”. Well. The best way Image display works in Colab, even with a low GPU:

  1. Make sure your drive is Mounted before this cell. After Mounting, place in a code cell:

  2. from IPython.display import Image display(Image('/content/drive/MyDrive/image/iris/iris.png', width=200, height=100))

  3. I keep a directory of Images and made sub-directories for each problem, like IRIS. Now you can manage everything using HTML codes. As I said, Google gained this Internet predominance from its historical infrastructure and not, I think, algorithmic innovations. Now it dominates all Page ranking with the 2021 Ranking Updates, hence the entire world of Digital Marketing. Please accept my apologies for my “Yak..Yak”. Professor Dey

Upvotes: -2

NeStack
NeStack

Reputation: 2014

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. You might also want to check out this question

enter image description here

Upvotes: -2

korakot
korakot

Reputation: 40828

Your code should work, but you need to upload example.png to Colab first.

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

To upload it, open the tab on the left, select 'Files' then 'Upload'

Upvotes: 9

Related Questions