问题描述
我正在使用 Google Colaboratory 笔记本.我将一个名为 bp.png
的文件上传到工作目录中,我可以通过在代码单元中运行 !ls
来查看该文件.然后我尝试这段代码以查看降价单元格中的图像:
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">
但运行后 Colab 笔记本的单元格保持为空(标题除外),尽管如果我在本地 Jupyter 笔记本中运行此图像,图像确实会出现在该本地笔记本的单元格中.
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.
更新:
我知道我可以使用上传到工作目录的文件,因为我上传的自定义 .py
文件可以毫无问题地导入到我的 Colab 笔记本中.例如,我可以上传一个文件 py_file.py
,然后在 Colab notebook 中使用它,就像 from py_file import some_function
一样,它可以工作.
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.
推荐答案
试试这个
from IPython.display import Image
Image('bp.png')
您也可以设置宽度和高度
You can set width and height as well
Image("bp.png", width=100, height=100)
要显示1张以上的图片,需要调用display.(只有 1 张图片是自动的)
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'))
2019 年 1 月更新
把你的图片放在/usr/local/share/jupyter/nbextensions/
然后从/nbextensions/
中显示出来,例如
Then display it from /nbextensions/
, e.g.
%%html
<img src='/nbextensions/image.png' />
这篇关于如何从上传的 png 文件在 Google Colaboratory 笔记本单元格中打开图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!