from PIL import Image
import numpy as np
import pandas as pd
train=pd.read_csv('\MnistData\train.csv')#讀取Mnist的數據
t=np.reshape(train.iloc[:1,1:].values,(28,28))#轉換爲28,28格式,取第一行,以及第一列之後的數據,列1是標籤行
im=Image.fromarray(t.astype(np.uint8))#將數據轉換爲圖片
im.save('1.png')#保存圖片
im.show()
im=Image.open('1.png')#打開圖片
im=im.convert("L")#灰度
data=im.getdata()#獲取圖片數據
data=np.matrix(data,dtype='uint8').reshape(28,28)#轉變形狀爲28,28
df=pd.DataFrame(data)#轉變爲dataframe
writer=pd.ExcelWriter('picture.xlsx')#創建一個excel書寫器
df.to_excel(writer,'page1')#page1是sheet的名稱
writer.save()
writer.close()
del im,data,df #釋放內存
print("svae to excel suessfull")