Created
June 23, 2018 19:31
-
-
Save shang-vikas/3670a06f0e4bfc1a52f4847ac3d31c78 to your computer and use it in GitHub Desktop.
convert csv data to jpg images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment