# 加载图片
data = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = data.load_data()
plt.imshow(x_train[0], cmap='gray')
# 创建10个文件夹存放每一类图片
for i in range(10):
os.makedirs(f"../datas/mnist/train/{i}")
os.makedirs(f"../datas/mnist/test/{i}")
# 保存图片并生成图像列表
# 训练集数据
with open("../datas/mnist/train/image_list.txt", 'w') as img_list:
i = 1
for img, label in zip(x_train, y_train):
img = Image.fromarray(img) # 将array转化成图片
img_save_path = f"../datas/mnist/train/{label}/{i}.jpg" # 图片保存路径
img.save(img_save_path) # 保存图片
img_list.write(img_save_path + "\t" + str(label) + "\n")
i += 1
# 测试集数据
with open("../datas/mnist/test/image_list.txt", 'w') as img_list:
i = 1
for img, label in zip(x_test, y_test):
img = Image.fromarray(img) # 将array转化成图片
img_save_path = f"../datas/mnist/test/{label}/{i}.jpg" # 图片保存路径
img.save(img_save_path)
img_list.write(img_save_path + "\t" + str(label) + "\n")
i += 1