Windows reads and saves images containing Chinese paths (python)

 

         When reading an image with a Chinese path under Windows, the error is always reported as garbled Chinese characters, and the image cannot be read and written. After searching around, the method that can successfully solve this problem without converting the encoding format is to use the cv2 library to read and write .

 
import cv2
import  numpy as np
 

#读取有中文路径的图像
file_path = 'D:/测试/测试_in.png'
img = cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1)




#保存成含有中文路径的图像
out_path = 'D:/测试/测试out.png'
img1 = cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1)
cv2.imencode('.jpg', img1 )[1].tofile(out_path)
 
 

ps: The pro-test is effective and can solve the problem of Chinese garbled characters and inability to read and write.

It's not easy to organize, welcome to one-click three links! ! !

Guess you like

Origin blog.csdn.net/qq_38308388/article/details/130398880