[英]Syntax error when trying to display a local image in jupyter notebook
Here is some diagnostics you can make using Python itself 这是您可以使用Python本身进行的一些诊断
import os
filename = 'img/x.png'
print(os.path.abspath(filename))
Does this command print the expected path? 此命令是否打印预期路径? If not, fix filename accordingly. 如果不是,请相应地修复文件名。
print(os.path.exists(filename))
Is this "True"? 这是真的”? If not, check that the file exists. 如果不存在,请检查文件是否存在。
For the following test you need OpenCV. 对于以下测试,您需要OpenCV。 If you don't have it, you need to install opencv-python
, eg using pip install opencv-python
. 如果没有,则需要安装opencv-python
,例如,使用pip install opencv-python
。
import cv2
im = cv2.imread(filename)
print(im.shape)
This should print a tuple with elements image height, image width and number of channels (for a color image). 这将打印一个元组,其中包含元素图像高度,图像宽度和通道数(对于彩色图像)。 If it doesn't, the image is most probably corrupted. 如果没有,则图像很可能已损坏。
You can also display the image using matplotlib: 您还可以使用matplotlib显示图像:
import matplotlib.pyplot as plt
plt.figure()
plt.imshow(im)
plt.show()
If you see wrong colors, probably matplotlib displays it uses a wrong palette or wrong channel order (RGB instead of BGR ets.) Take a look at plt.imshow
help to find, how to fix it. 如果您看到错误的颜色,则可能是matplotlib显示它使用了错误的调色板或错误的通道顺序(RGB而不是BGR plt.imshow
)。请查看plt.imshow
帮助以查找如何修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.