import cv2 import os import glob # 根目录路径 root_path = r'C:\Users\KDAN\Desktop\work\exchange\font_style_classification\Fonts_img_Dataset\windows_1\chinese' save_path = r'C:\Users\KDAN\Desktop\work\exchange\font_style_classification\Fonts_img_Dataset\windows_1\chinese_gray' list_path = os.listdir(root_path) for k, names in enumerate(list_path): print(k, names) # if names.endswith('.txt'): # continue # if not os.path.exists(save_path + '\\' + names): # os.makedirs(save_path + '\\' + names) bmp_path = glob.glob(r'C:\Users\KDAN\Desktop\work\exchange\font_style_classification\Fonts_img_Dataset\windows_1\chinese\{}\*jpg'.format(names)) for i, path_bmp in enumerate(bmp_path): if path_bmp.endswith('.txt'): continue # print(path_bmp) a = os.path.split(path_bmp) name = os.path.basename(a[0]) # print(name) img = cv2.imread(path_bmp) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # cv2.imshow('figure', gray) # cv2.waitKey(0) cv2.imwrite('C:/Users/KDAN/Desktop/work/exchange/font_style_classification/Fonts_img_Dataset/windows_1/chinese_gray/{}/{}_{}.jpg'.format(names, name, i), gray)