我正在尝试使用opencv渲染图像。
这是代码
from matplotlib import pyplot as plt
import cv2
img = cv2.imread('xwdsh.png')
plt.imshow(img)
plt.show()
这段代码在jupyter中运行良好,但在Python CLI模式下导致以下错误。
$ /Users/jay/anaconda3/envs/tf/bin/pythonw /afs/afs.sxl/cv.dl/face_detection_image.py
Traceback (most recent call last):
File "/afs/afs.sxl/cv.dl/face_detection_image.py", line 5, in <module>
plt.imshow(img)
File "/Users/jay/anaconda3/envs/tf/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2653, in imshow
resample=resample, url=url, data=data, **kwargs)
File "/Users/jay/anaconda3/envs/tf/lib/python3.6/site-packages/matplotlib/__init__.py", line 1785, in inner
return func(ax, *args, **kwargs)
File "/Users/jay/anaconda3/envs/tf/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 5472, in imshow
im.set_data(X)
File "/Users/jay/anaconda3/envs/tf/lib/python3.6/site-packages/matplotlib/image.py", line 642, in set_data
raise TypeError("Image data cannot be converted to float")
TypeError: Image data cannot be converted to float
答案 0 :(得分:1)
用实际路径替换完整/路径,例如/afs/afs.sxl/cv.dl/xwdsh.png
img = cv2.imread('full/path/xwdsh.png')
plt.imshow(img)
plt.show()