-
Notifications
You must be signed in to change notification settings - Fork 13
/
image_to_numpy.py
64 lines (44 loc) · 1.53 KB
/
image_to_numpy.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
from PIL import Image
import numpy as np
#Directory containing images you wish to convert
input_dir = "/Users/14/Documents/deep-learning-project/data/output_images/"
directories = os.listdir(input_dir)
index = 0
index2 = 0
for folder in directories:
#Ignoring .DS_Store dir
if folder == '.DS_Store':
pass
else:
print folder
folder2 = os.listdir(input_dir + '/' + folder)
index += 1
for image in folder2:
if image == ".DS_Store":
pass
else:
index2 += 1
im = Image.open(input_dir+"/"+folder+"/"+image) #Opening image
im = (np.array(im)) #Converting to numpy array
try:
r = im[:,:,0] #Slicing to get R data
g = im[:,:,1] #Slicing to get G data
b = im[:,:,2] #Slicing to get B data
if index2 != 1:
new_array = np.array([[r] + [g] + [b]], np.uint8) #Creating array with shape (3, 100, 100)
out = np.append(out, new_array, 0) #Adding new image to array shape of (x, 3, 100, 100) where x is image number
elif index2 == 1:
out = np.array([[r] + [g] + [b]], np.uint8) #Creating array with shape (3, 100, 100)
if index == 1 and index2 == 1:
index_array = np.array([[index]])
else:
new_index_array = np.array([[index]], np.int8)
index_array = np.append(index_array, new_index_array, 0)
except Exception as e:
print e
print "Removing image" + image
os.remove(input_dir+"/"+folder+"/"+image)
print index
np.save('X_train.npy', out) #Saving train image arrays
np.save('Y_train.npy', index_array) #Saving train labels