“TypeError:图像数据无法转换为浮点数”在python27中未找到matplotlib

时间:2017-04-04 07:24:05

标签: python numpy matplotlib

import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('messi5.jpg',0)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()

当输入代码如图所示时,shell将错误输出为标题

"TypeError: Image data can not convert to float"

我不知道如何解决问题,我希望得到答案。非常感谢你

1 个答案:

答案 0 :(得分:1)

您的代码运行良好,所以我认为文件路径不正确,请尝试运行此代码以查看文件路径:

import os
import cv2
from matplotlib import pyplot as plt

path='messi5.jpg'

if os.path.isfile(path):
    img = cv2.imread(path,0)
    plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
    plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
    plt.show()
else:
    print("file not exists")