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

2024-04-27 16:52:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我导入了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

我正在使用Pycharm作为我的ide。


Tags: theto代码图像imagenumpy绘图img
2条回答

你有点模棱两可

here do we need to give the location of the file or the file directly

不,你不需要。你需要使用图像库来读取图像。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()

在matplotlib的文档Image tutorial上阅读更多内容

imganumpy数组的类型是否正确?

如果您使用枕头等读取图像,并且有一个图像对象,则必须从中获取numpy数组(img.getdata()

X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4)

Display the image in X to current axes. X may be a float array, a uint8 array or a PIL image. If X is an array, it can have the following shapes:

MxN – luminance (grayscale, float array only) MxNx3 – RGB (float or uint8 array) MxNx4 – RGBA (float or uint8 array) The value for each component of MxNx3 and MxNx4 float arrays should be in the range 0.0 to 1.0;

要么将img规范化,使其介于0.0和1.0之间,要么将其转换为uint8(img=np.array(img, dtype=np.uint8))。

相关问题 更多 >