# -*- encoding:utf-8 -*-
import numpy as np
from PIL import Image
import random


def read_data(path):
	with open(path) as f :
		lines=f.readlines()
	lines=random.sample(lines,int(len(lines)/1000))
	lines=[eval(line.strip()) for line in lines]
	X,Y=zip(*lines)
	X=np.array(X)
	X=X.reshape(-1,28*28)
	Y=np.array(Y)
	return X,Y


def plot(x,width,height,path):
	img=[[0 for _ in range(0,width) ] for _ in range(0,height)]
	for i in range(0,height):
		for j in range(0,width):
			img[i][j]=x[i*height+j]
	img=np.array(img).astype('uint8')
	new_im = Image.fromarray(img)
	new_im.save(path)


train_x,train_y = read_data("train_data")
train_x_10,train_y_10 = read_data("train_data_10")
train_x = np.concatenate((train_x,train_x_10))
train_y = np.concatenate((train_y,train_y_10))
count=0


for i in range(0,len(train_x)):
	print(count)
	path="img/{}-{}.png".format(train_y[i],count)
	print(path)
	count+=1
	plot(train_x[i],28,28,path)

