from scipy.misc import imsave import os import numpy as np import pandas as pd from keras.datasets import mnist def convert_to_jpg(x,y,df_type='train'): if df_type=='train': path = os.path.abspath('./digit-recognizer/train') if not os.path.isdir(path): os.mkdir(path) elif df_type=='test': path = os.path.abspath('./digit-recognizer/test') if not os.path.isdir(path): os.mkdir(path) c=0 for i in range(y.shape[0]): name = 'image' + str(i) + '_' +str(y[i]) + '.jpg' imsave(os.path.join(path,str(name)),x[i]) c+=1 if c%5000==0: print('{} images written'.format(c)) if __name__=='__main__': (x_train,y_train),(x_test,y_test) = mnist.load_data() ## the above command might take some time as it will download the data(68 mb approx.) convert_to_jpg(x_train,y_train,'train') convert_to_jpg(x_test,y_test,'test')