-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_preprocess.py
More file actions
45 lines (34 loc) · 1.09 KB
/
data_preprocess.py
File metadata and controls
45 lines (34 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
import numpy as np
import tensorflow as tf
from PIL import Image
dir = "./train-jpg/"
width = 64
height = 64
l = width*height
train_jpg_list = os.listdir(dir)
input_count = len(train_jpg_list)
cut_num = 250
remain = input_count % cut_num
split = input_count // cut_num
del train_jpg_list
def ImageToArray(file,width,height):
'''3 color channels, 1*length'''
img_raw = Image.open(file)
img = img_raw.resize((width, height), Image.ANTIALIAS)
width,height = img.size
data = img.convert("RGB").getdata()
data = np.array(data, dtype='int')
result = np.reshape(data,(1,width*height*3))
return result
for j in range(split):
if j+1 == split:
count = cut_num + remain
else: count = cut_num
input_images = np.array([[0]*l*3 for i in range(count)])
for i in range(j*cut_num,j*cut_num+count):
file = dir + "train_%s.jpg" % i
data = ImageToArray(file,width,height)
if i % 10 == 0: print("Have completed " + str(i))
input_images[i-j*cut_num] = data
np.savetxt( "train-jpg-all-"+str(j)+".txt",input_images,fmt="%d")