简体   繁体   English

如何在 Google Colab 中的笔记本中显示图像(如在 Anacondan Jupyter Notebook 中)?

[英]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.我正在写一本关于紧绑定理论的 Google Colab Notebook。 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我想在降价单元格或代码单元格中显示图像,就像在 Anaconda 中可以使用以下代码一样

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:我试过在 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/将您的图像放在 /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.编辑:我现在意识到该目录适用于 LINUX 操作系统。 Any way I could do something equivalent in Windows (My computer operates Windows)无论如何我可以在 Windows 中做一些等效的事情(我的电脑运行 Windows)

Your code should work, but you need to upload example.png to Colab first.您的代码应该可以工作,但您需要先将example.png上传到 Colab。

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

To upload it, open the tab on the left, select 'Files' then 'Upload'要上传它,请打开左侧的选项卡,选择“文件”,然后选择“上传”

One can also display images in the markdown/Text cell in Colab.还可以在 Colab 的 Markdown/Text 单元格中显示图像。 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你可能还想看看这个问题

在此处输入图片说明

Google Colab, in my opinion, is probably the most connected platform for Python and Data Science related programming.在我看来,Google Colab 可能是 Python 和数据科学相关编程中连接最紧密的平台。 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. Google 主导着 95% 以上的互联网搜索活动,Colab 与 Google Data Studio for Big data Analytics、Google Search Console 以及来自 Google 的所有数据和算法存储库相连。 Thus, you can pull the entire internet from Colab, so to speak.因此,可以这么说,您可以从 Colab 拉取整个互联网。 You are thinking “Cut to the chase.”.你在想“切入正题”。 Well.好。 The best way Image display works in Colab, even with a low GPU:图像显示在 Colab 中的最佳工作方式,即使 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)) 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.我保留了一个图像目录,并为每个问题创建了子目录,例如 IRIS。 Now you can manage everything using HTML codes.现在您可以使用 HTML 代码管理一切。 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.现在,它在 2021 年排名更新中主导了所有页面排名,因此是整个数字营销世界。 Please accept my apologies for my “Yak..Yak”.请接受我为我的“Yak..Yak”道歉。 Professor Dey戴教授

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM