先把资源下载后,解压缩;
在当前文件夹中创建data文件夹后,在data文件夹内创建0-9,共十个文件夹;
import os
import struct
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
def load_mnist(path, kind='train'):
"""Load MNIST data from `path`"""
labels_path = os.path.join(path,
'%s-labels.idx1-ubyte'
% kind)
images_path = os.path.join(path,
'%s-images.idx3-ubyte'
% kind)
with open(labels_path, 'rb') as lbpath:
magic, n = struct.unpack('>II',
lbpath.read(8))
labels = np.fromfile(lbpath,
dtype=np.uint8)
with open(images_path, 'rb') as imgpath:
magic, num, rows, cols = struct.unpack('>IIII',
imgpath.read(16))
images = np.fromfile(imgpath,
dtype=np.uint8).reshape(len(labels), 784)
return images, labels
path = 'F:\\Codes\\2021_Codes\\minst_Num\\minst'
X_train, y_train = load_mnist(path, kind='train')
for i in range(0, 60000):
A = X_train[i].reshape(28, 28)
im = Image.fromarray(A)
path_fig = "...\\data\\" + str(y_train[i]) + "\\"
name = str(i) + ".png"
im.save(path_fig+name)