windows下使用python + opencv读取含有中文路径的图片 和 把图片数据保存到含有中文的路径下

1 windows下读取含有中文路径的图片

1、读取含有中文路径的图片

在windows下使用cv2.imread(img_path)读取含有中文路径的图片,如下:

import cv2

img_path = r"D:\dataset\巡检数据\Camera1-20220414\000000.jpg"
img = cv2.imread(img_path)
print(img.shape)

会报错:AttributeError: 'NoneType' object has no attribute 'shape',这是因为没有正确读取到图片,显示是一个空数据!

2、解决方法:使用cv2.imdecode()

import cv2
import numpy as np

img_path = r"D:\dataset\巡检数据\Camera1-20220414\000000.jpg"
img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), -1)
print(img.shape)

2 windows下把图片数据保存到中文目录下

1、如果使用cv2.imwrite(img_path) 把图片保存到中文路径下,虽然程序没有保存,但是中文目录下是空的,图片并没有正确保存到中文目录下!

2、这在python3下不支持中文路径的编码,解决方式是使用cv2.imencode()

import cv2

img = cv2.imread("./image/test.jpg")
save_img_path = r"D:\dataset\巡检数据\Camera1-20220414\000000.jpg"
cv2.imencode('.jpg', img)[1].tofile(save_img_path)

猜你喜欢

转载自blog.csdn.net/weixin_41010198/article/details/126094955