python 报错 SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position xx 解决方法

今天用cv2打开图片文件的时候运行报错,显示:SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position

完整代码:

# -*- coding: utf-8 -*-
# @TIME     : 2020/9/15 11:08
# @Author   : Chen Shan
# @Email    : [email protected]
# @File     : test.py
# @Software : PyCharm

import cv2

img = cv2.imread("C:\Users\Administrator\Pictures\14.jpg")
cv2.imshow("Image", img)
cv2.waitKey (0)
cv2.destroyAllWindows()
print(cv2.__version__)

报错截图:
报错

字面翻译下就是编译器没能成功识别某些字符
错误

解决方法:我用的是最简单的一种,在字符串前面加上一个r,表示保持字符原始值

img = cv2.imread(r"C:\Users\Administrator\Pictures\14.jpg")

应该还可以把反斜杠变成双方斜杠,或者是替换为正斜杠

即:

img = cv2.imread("C:\\Users\\Administrator\\Pictures\\14.jpg")
img = cv2.imread("C:/Users/dministrator/Pictures/14.jpg")

猜你喜欢

转载自blog.csdn.net/qq_44702847/article/details/108596616