在批量重命名的时候,保存图片时,带有中文的字符会保存乱码。查阅资料后,找到解决方法。
import cv2 import os import numpy as np path = 'E:\Personal\Desktop\rename\zaian\' names = os.listdir(path) for name in names: #img = cv2.imread(path + name)# #img=cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1)# img = cv2.imdecode(np.fromfile(path + name , dtype=np.uint8),-1) x = name.split('-',2) newname = str(x[0]) +'-'+ x[2] #cv2.imwite('E:\Personal\Desktop\rename\w\'+newname,img) #cv2.imencode('.jpg',src)[1].tofile(filename) cv2.imencode('.png',img)[1].tofile('E:\Personal\Desktop\rename\w\'+newname)