-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageProcessor.py
55 lines (38 loc) · 1.13 KB
/
ImageProcessor.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
from PIL import Image
import numpy as np
def get_processes_image(file_path):
image = get_image(file_path)
image = process_image(image)
return image
def get_normalized_vector(file_path):
image = get_processes_image(file_path)
return to_normalized_vector(image)
def get_image(file_path):
image = Image.open(file_path)
return image
def process_image(image):
image = image.convert(mode='L')
image = make_square(image)
image = image.resize((32,32))
return image
def to_normalized_vector(image):
array2D = np.array(image,'f') / 255
vector = np.concatenate(array2D)
return vector
def to_vector(image):
array2D = np.array(image,'f')
vector = np.concatenate(array2D)
return vector
def make_square(image):
width, height = image.size
if width != height:
if width > height:
new_size = height
else:
new_size = width
left = (width - new_size)/2
top = (height - new_size)/2
right = (width + new_size)/2
bottom = (height + new_size)/2
image = image.crop((left, top, right, bottom))
return image