python opencv 讀寫中文路徑下的中文文件名的圖片
import cv2 #定義一個叫cv_imread的函數來讀取中文路徑的圖片,filePath是圖片的完整路徑 def cv_imread(filePath): #讀取中文路徑的圖片 cv_img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),cv2.IMREAD_UNCHANGED) #imdecode讀取的圖像默認會按BGR通道來排列圖像矩陣,如果后續需要RGB可以通過如下轉換 #cv_img=cv2.cvtColor(cv_img,cv2.COLOR_BGR2RGB) return cv_img #定義一個叫cv_imwrite的函數來往中文路徑寫入img圖片,filePathName是待寫入的文件夾和圖片名字組成的完整 #路徑,如filePathName = C:\\user\\Desktop\\test.jpg def cv_imwrite(filePathName, img): try: _, cv_img = cv2.imencode(".jpg", img)[1].tofile(filePathName) return True except: return False