问题描述
早上好
设定 :
Windows 7(我知道)
崇高文字3
Python 3.6
我的问题 :
我的文件中有一些28x28的图片,例如其中一个位于相对路径“ MyDir / myimage.png”
我正在尝试使用IPython包中的显示模块显示示例图像
from IPython.display import display,Image
img=Image(filename='MyDir/myimage.png')
display(img)
问题在于,它没有输出图形中的图像,而是仅在控制台中输出对象img的类型(显示仅在控制台中显示)。
输出:
<IPython.core.display.Image object>
有任何想法吗?
1楼
IPython(交互式python)在控制台中无法正常工作。
但是,例如在jupyter笔记本电脑中,一切进展顺利:
还有 ,听起来像只是一个更像控制台的jupyter笔记本。 我没有检查过,因为在vscode和jupyter笔记本之间,到目前为止我还不错。
要了解更多信息,您可以搜索jupyter和qt之类的工具之间的比较。
并查看IPython文档。
但是,如果您只希望该死的东西向您显示图像,则可以在jupyter等中运行python脚本。
或使用PIL
作为提及的 。
另外,您可以使用matplotlib.pyplot
代替显示图像(这在控制台和 jupyter中均有效):
from matplotlib.pyplot import figure, imshow, axis, show
from matplotlib.image import imread
import numpy as np
import os
imageDirectory = "c:\\some\\directory\\of\\images"
list_of_files = np.array(os.listdir(imageDirectory))[0:20] # just show the first 20 images
fig = figure()
number_of_files = len(list_of_files)
for i in range(number_of_files):
a=fig.add_subplot(1,number_of_files,i+1)
image = imread(os.path.join(imageDirectory, list_of_files[i]))
imshow(image,cmap='Greys_r')
axis('off')
show()