-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoords_to_img.py
executable file
·133 lines (115 loc) · 4.31 KB
/
coords_to_img.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
# Convert list of coordinates into pictures.
import numpy as np
import PIL
import math
def make_image_from_coords(coordinates, name):
x, y = [i[0] for i in coordinates], [i[1] for i in coordinates]
max_x, max_y = max(x), max(y)
min_x, min_y = min(x), min(y)
image = np.zeros((max_y - min_y + 1, max_x - min_x + 1))
for i in range(len(coordinates)):
image[y[i] - min_y][x[i] - min_x] = int(1)
im = PIL.Image.fromarray(image.astype('uint8')*255)
# save as binary array text file
np.savetxt('lao_text/' + name + '.txt', image, fmt='%d')
# save as bitmap image
im.save('lao_images/' + name + '.bmp')
# thinning
# return width and height of image
return (max_x - min_x, max_y - min_y)
def print_pixels_in_image (image, max_x, max_y):
for i in range(max_y + 1):
for j in range(max_x + 1):
print str(image[i][j]) + " "
print "\n"
# extract coordinates for characters in char_lst
def extract_coordinates_from_file(filename, char_lst):
f = open(filename, 'r')
line_num = 0
# for each person
coordinates_list = []
# one for each character
coordinates = []
# interpolate in cases where x-coords or
# y-coords differ from prev by this threshold
dist_to_interpolate = 2
prev_x = None
prev_y = None
char_index = 0
for line in f:
line_num += 1
# skip first ten lines
if line_num < 10:
continue
tokens = line.split(" ")
if len(tokens) == 3 and char_index in char_lst:
new_x = int(tokens[1])
new_y = int(tokens[2])
if prev_x and prev_y:
# interpolate if separated from prev x or y coord
if abs(new_x - prev_x) == dist_to_interpolate or \
abs(new_y - prev_y) == dist_to_interpolate:
mid_x = prev_x + int(round((new_x - prev_x)/2))
mid_y = prev_y + int(round((new_y - prev_y)/2))
coordinates.append((mid_x, mid_y))
coordinates.append((new_x, new_y))
prev_x, prev_y = new_x, new_y
# reached end of character line
if len(tokens) == 1:
if len(coordinates) > 0 and char_index in char_lst:
coordinates_list.append(coordinates)
coordinates = []
char_index += 1
# print tokens
# last character's coordinates
if char_index in char_lst and not len(coordinates) == 0:
coordinates_list.append(coordinates)
return coordinates_list
def make_images_from_file(filename, char_lst):
file_token = filename.split('/')[-1].split('.')[0]
coordinate_list = extract_coordinates_from_file(filename, char_lst)
item = 0
img_sizes = []
for i, lst in enumerate(coordinate_list):
img_sizes.append(make_image_from_coords(lst, file_token + "_" + str(char_lst[i])))
return img_sizes
def convert(fname, person_lst, char_lst):
# test case
# print extract_coordinates_from_file("Distribution/Tests/interpolate_test.txt")
img_sizes = []
for pind in person_lst:
img_sizes += make_images_from_file(fname + "000" + str(pind) + ".txt", char_lst)
return img_sizes
"""
for i in range(10, 52):
make_images_from_file("Distribution/Data/00" + str(i) + ".txt")
"""
# scales a picture to fit a fixed height and width, maintaining aspect ratio
def fit_to_size(fname, basewidth, baseheight):
# Rescale so width = sizex (keep aspect ratio)
old_im = PIL.Image.open(fname)
wpercent = (basewidth/float(old_im.size[0]))
# we want the highest predicted hsize
hsize = int((float(old_im.size[1])*float(wpercent)))
new_im = old_im.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
# Add margins so image is at fixed height and width
old_im = new_im
old_size = old_im.size
new_size = (basewidth, baseheight) # new image size is exactly 800 x 800, centered
new_im = PIL.Image.new("RGB", new_size) ## luckily, this is already black!
new_im.paste(old_im, ((new_size[0]-old_size[0])/2, (new_size[1]-old_size[1])/2))
new_im.save(fname)
def convert_images(person_lst, char_lst):
img_sizes = convert("Distribution/Data/", person_lst, char_lst)
# determine the width and height each image should be scaled to
x, y = [i[0] for i in img_sizes], [i[1] for i in img_sizes]
basewidth = max(x) # minimum basewidth
multipliers_lst = map(lambda x: basewidth/float(x), x)
height_lst = [mult*height for mult, height in zip(multipliers_lst, y)]
baseheight = int(math.ceil(max(height_lst)))
# scale each image
for person_index in person_lst:
for char_index in char_lst:
filename = "lao_images/000" + str(person_index) + "_" + str(char_index) + ".bmp"
fit_to_size(filename, basewidth, baseheight)