import os import cv2 import numpy as np # 读入中文命名图片 def cv_imread(in_path): cv_img = cv2.imdecode(np.fromfile(in_path, dtype=np.uint8), -1) # -1表示cv2.IMREAD_UNCHANGED return cv_img # 写入中文命名图片 def cv_imwrite(out_path, img_np): cv2.imencode('.png', img_np)[1].tofile(out_path) in_dir = 'C:/Users/Shuai/Desktop/railway/station/' out_dir = 'C:/Users/Shuai/Desktop/railway/station2/' # 例子:中文名jpg转png图片 for img_name in os.listdir(in_dir): img = cv_imread(in_dir + img_name) new_name = img_name.replace('.jpg', '.png') cv_imwrite(out_dir + new_name, img) print(new_name)