在 ipython 中使用 matplotlib 显示图像

Displaying image with matplotlib in ipython

如何在 ipython 中使用 matplotlib 显示使用 numpy 包导入的图像?

使用命令应该很容易

import numpy as np
import matplotlib.pyplot as plt

im = np.array(Image.open('image.jpg'))

plt.imshow(im)

但是图像不显示,我只是得到输出

<matplotlib.image.AxesImage at 0x7fb38e10ff10>

您必须调用 plt.show() 才能实际调出 windows。 您可以使用 交互模式 来解决这个问题。但对于脚本,最好在完成所有绘图命令后简单地调用 show()

在 IPython 或 Jupyter notebooks 中,如果您想在笔记本中而不是在单独的 window 中以内联方式显示图像,请执行下面显示的代码。

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

im = np.array(Image.open('image.jpg'))

plt.imshow(im)
plt.show()