我在“test”文件夹中有一堆图像,我在文件中有一个 python 代码来显示这些图像。这是代码
import torch
from IPython.display import Image, clear_output
import matplotlib
import glob
import PIL
from IPython.display import Image, display
from io import BytesIO
print("hi")
for imageName in glob.glob('test/images/*.jpg'): #assuming JPG
print("in \n")
display(Image(filename=imageName)) #displaying successfully in colab
print("out")
print("\n")
这是我在 ubuntu 终端中运行 python 文件时的输出。我没有打开其他选项卡来显示图像图像。我在堆栈溢出中尝试了其他答案,但没有奏效。但是,这些图像在 colab 的输出中是可见的。
最佳答案
您在终端中运行,终端只能显示文本。
因此display(Image(...))
无法显示您的图像。display
和 Image
是真正以 IPython/Jupyter 为中心的实用程序,在使用纯 Python 时无法完全运行。
如果你想在终端运行时显示图像,你需要使用 pillow 之类的东西。或 matplotlib并要求他们打开一个带有图像的新窗口,根据您的操作方式,您可能需要关闭窗口以使程序继续执行。
也可以在本地运行jupyter notebook
(或 jupyter lab
)打开应支持图像的 Web 界面。
关于python - 无法在 ubuntu 中显示 IPython 对象,但在 colab 输出中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66775474/