Html 如何从上传的 png 文件在 Google Colaboratory 笔记本单元格中打开图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49478791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I open images in a Google Colaboratory notebook cell from uploaded png files?
提问by Sergey Zakharov
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:
我正在使用 Google Colaboratory 笔记本。我上传了一个名为bp.png
工作目录的文件,我可以通过!ls
在代码单元中运行来看到该文件在那里。然后我尝试这段代码来查看降价单元格中的图像:
<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.
但是运行后 Colab 笔记本的单元格保持为空(标题除外),尽管如果我在本地 Jupyter 笔记本中运行它,图像确实会出现在该本地笔记本的单元格中。
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.
我知道我可以使用上传到工作目录的.py
文件,因为我上传的自定义文件可以毫无问题地导入到我的 Colab 笔记本中。例如,我可以上传一个文件py_file.py
,然后在 Colab 笔记本中像在 中一样使用它from py_file import some_function
,并且它可以工作。
回答by korakot
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)
要显示 1 张以上的图像,您需要调用 display. (只有 1 张图像是自动的)
from IPython.display import Image, display
display(Image('1.png'))
display(Image('2.png'))
Update jan/2019
2019 年 1 月更新
Put your image in /usr/local/share/jupyter/nbextensions/
把你的图片放进去 /usr/local/share/jupyter/nbextensions/
Then display it from /nbextensions/
, e.g.
然后显示它/nbextensions/
,例如
%%html
<img src='/nbextensions/image.png' />