初學python 解壓手寫訓練圖片

import  numpy as np
from matplotlib import pyplot as plt
from PIL import Image
import  os




def load_data(path='mnist.npz'):
    f = np.load(path)

    x_train, y_train = f['x_train'], f['y_train']
    x_test, y_test = f['x_test'], f['y_test']
    f.close()
    return (x_train, y_train), (x_test, y_test)

(x_train ,y_train),( x_test ,y_test)=load_data("C:/Python35/學習深度/KARES/mnist.npz")

def showImg(mg,index):

    #plt.imshow(mg[index])
    #plt.show()
    print(mg[index].dtype)
    im = Image.fromarray(mg[index].astype(mg[index].dtype))
    if os.path.isdir("./"+str(y_train[index]))==False:
       os.mkdir("./"+str(y_train[index]))
    im.save("./"+str(y_train[index])+"/"+str(index)+".jpg")

print(x_train.ndim)


print(x_train.shape[0])

for _is in range(x_train.shape[0]):
    print(_is)
    showImg(x_train, _is)
    #print(x_train[_is])


print(y_train.shape)

print(x_test.shape)
print(y_test.shape)

這裏寫圖片描述
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章