if __name__ == '__main__':
rootDir = r'G:PYwenjianvenvfenkuaitu11'
imgStorePath = rootDir + '_BMP/' # 存储路径
if (not os.path.exists(imgStorePath)):
os.makedirs(imgStorePath) #创建目录
a_list = fnmatch.filter(os.listdir(rootDir),'*.png')
for i in range(len(a_list)):
path = rootDir+'/'+a_list[i]
# 开始读取
img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), -1)
# 直接调用.tofile(_path),我这里显示权限拒绝,所用系统自带的文件写入
img_encode = cv2.imencode('.bmp',img)[1]
# img = cv2.imread(path)
t = a_list[i]
t = t[:-4] # 拿到图片名
with open(imgStorePath + t + '.bmp', 'wb') as f: #写入
f.write(img_encode)