关于python读写图片遇到中文路径的问题
cv2.imread
Python进行图片处理,第一步就是读取图片,一般路径都是英文名,但必不可免会遇到中文问题。
如下:
s=r'20210414_信号灯2658_0.jpg'
img_cv = cv2.imread(s) # 读取数据
print(img_cv.shape)
# AttributeError: 'NoneType' object has no attribute 'shape'
解决方法使用imdecode:
soure=r'20210414_信号灯2658_0.jpg'
img = cv2.imdecode(np.fromfile(soure, dtype=np.uint8), -1)
print(img.shape)
#(20, 18, 3)
cv2.imwrite
写入图片遇到的问题就是,不报错,但没有图片。
#source 是输入路径,dest是输出路径
img = cv.imdecode(np.fromfile(soure, dtype=np.uint8), -1)
# print(img)
cv.imwrite(dest, img)
dest为输出路径,图片名也必须为英文名。否则虽然不报错,但是不能成功写入。