如何使用 Python 在绘图中显示图像?

Sah*_*hil 0 python image matplotlib

我导入了 matplotlib.pyplot 和 NumPy

我想将桌面上的图像显示到绘图中,但我得到了一个TypeError.

代码 :

img = (image) ( here do we need to give the location of the file or the file directly)

imshow(img, extent=[-25,25,-25,25], cmap = cm.bone)
colorbar()


Error: TypeError: Image data can not convert to float
Run Code Online (Sandbox Code Playgroud)

我使用 Pycharm 作为我的 ide。

And*_*uri 5

你有点模棱两可

这里是需要给文件的位置还是直接给文件

不,你没有。您需要使用一些图像库来读取图像。img="C:\image.jpg"不读取图像!

例如,要读取“png”图像,您可以:

# Copypaste from docs
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
img=mpimg.imread('myimage.png')
# end
# from now on you can use img as an image, but make sure you know what you are doing!
imgplot=plt.imshow(img)
plt.show()
Run Code Online (Sandbox Code Playgroud)

在 matplotlib 的文档中阅读更多图像教程