import os
import re
import glob
import numpy as np
from PIL import Image
import scipy.io as io
src_dir = '/home/cgl/桌面/aa/'
save_dir = '/home/cgl/桌面/bb/'
all_file = os.walk(src_dir)
fileNum = 0
for root, dirs, files in all_file:
for file in files:
fileNum = fileNum + 1
print(src_dir+file)
file_ext = os.path.splitext(file)
front, ext = file_ext
img = Image.open(src_dir+file)
res = np.array(img, dtype='uint16')
if not os.path.exists(save_dir):
os.mkdir(save_dir)
np.save(save_dir+front+'.npy', res)
numpy_file = np.load(save_dir+front+'.npy')
io.savemat(save_dir+front+'.mat', {'data': numpy_file})
delete_command ='rm -rf '+save_dir+'*.npy'
print(delete_command)
os.system(delete_command)
print('共转化了'+ str(fileNum)+'张')