-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutilities.py
More file actions
192 lines (158 loc) · 6.37 KB
/
utilities.py
File metadata and controls
192 lines (158 loc) · 6.37 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import numpy as np
from math import ceil, floor
from random import randrange, uniform, sample
from PIL import Image
import aggdraw
def create_cmap(color_file):
"""
Creates an RGB color map out of a list of color values
Args:
color_file (string): A csv file of the color gradient used to generate the heightmaps.
Returns:
array: An array of RGB values representing the colors for each height in the collection's range of heights.
"""
colors = np.loadtxt(color_file, dtype=str)
cmap = []
for color in colors:
cmap.append(color.split(',')[0])
cmap = np.array(cmap, dtype=int)
return cmap
def height_to_color(cmap, height):
"""
Translates a building height value to a color, based on the given color map.
Args:
cmap (array): An array of RGB values representing the colors for each height in the collection's range of heights.
height (float): A building height.
Returns:
tuple: The (R, G, B) value that corresponds to the specific input height.
"""
if(height > len(cmap)-1):
color_value = 0
else:
modulo = height % 1
if(modulo) == 0:
color_value = cmap[int(height)]
else:
minimum = floor(height)
maximum = ceil(height)
min_color = cmap[minimum+1]
max_color = cmap[maximum+1]
color_value = min_color + ((min_color-max_color) * modulo)
return [color_value, color_value, color_value]
def get_poly_ids(polygons, status, indgen):
"""
A function that selects random polygons from an input list of polygons.
Args:
polygons (list): A list of polygons
random_genes (float, optional): Whether to select a random number of polygons, bypassing indgen.
indgen (float): The minimum percentage of input polygons to select.
Returns:
list: A list of polygon ids.
"""
mask = status.astype(bool)
valid_ids = list(np.arange(0, len(polygons))[mask])
poly_ids = sample(valid_ids, int(len(valid_ids) * indgen))
return poly_ids
def draw_polygons(polygons, grid_ids, colors, heights, im_size=(2500, 2500), b_color="white", fpath=None, grid_id=None):
if(grid_id):
image = Image.new("RGB", (250, 250), color=b_color)
draw = aggdraw.Draw(image)
# get displacement from origin
x_displacement = grid_id // 10
y_displacement = grid_id % 10
ids = np.where(grid_ids == grid_id)
polygons = polygons[ids]
colors = colors[ids]
heights = heights[ids]
for poly, color , height in zip(polygons, colors, heights):
# get x, y sequence of coordinates for each polygon
xy = poly.exterior.xy
coords = np.dstack((xy[1], xy[0])).flatten()
#bring everything to 250, 250
# x-coordinates
coords[1::2] = coords[1::2] - x_displacement*(im_size[0]//10)
# y-coordinates
coords[0::2] = coords[0::2] - y_displacement*(im_size[1]//10)
# create a brush according to each polygon color
if(height == 0.0):
brush = aggdraw.Brush((255, 255, 255), opacity=255)
else:
brush = aggdraw.Brush((color[0], color[1], color[2]), opacity=255)
draw.polygon(coords, brush)
image = Image.frombytes("RGB", (250, 250), draw.tobytes()).rotate(90)
else:
image = Image.new("RGB", im_size, color="white")
draw = aggdraw.Draw(image)
for poly, color, height in zip(polygons, colors, heights):
# get x, y sequence of coordinates for each polygon
xy = poly.exterior.xy
coords = np.dstack((xy[1], xy[0])).flatten()
# create a brush according to each polygon color
if(height == 0.0):
brush = aggdraw.Brush((255, 255, 255), opacity=255)
else:
brush = aggdraw.Brush((color[0], color[1], color[2]), opacity=255)
draw.polygon(coords, brush)
image = Image.frombytes("RGB", im_size, draw.tobytes()).rotate(90)
if(fpath):
image.save(fpath)
return draw, image
def rotate_input(pil_img, degrees, interval=512):
"""
Method to rotate image by `degrees` in a COUNTER-CLOCKWISE direction.
As some rotations cause the corners of the original image to be cropped,
the `interval` argument allows the image to expand in size.
Args:
pil_img (PIL.Image): A PIL image of the individual.
degrees (int): The degrees of rotation.
interval (int, optional): The interval to use while rotating the image. Defaults to 512.
Returns:
PIL image: A rotated image of the input individual.
"""
def next_interval(current):
c = int(current)
if c % interval == 0:
return c
else:
return interval * ((c // interval) + 1)
def paste_top_left_coords(rot_width, rot_height, exp_width, exp_height):
calc = lambda r, e: int((e - r) / 2)
return calc(rot_width, exp_width), calc(rot_height, exp_height)
if pil_img.mode != 'RGB':
pil_img = pil_img.convert('RGB')
degrees = degrees % 360
if degrees % 90 != 0:
rot_img = pil_img.rotate(
angle=degrees,
resample=Image.BICUBIC,
expand=1,
fillcolor=(255, 255, 255)
)
min_width, min_height = rot_img.size
exp_width = next_interval(min_width)
exp_height = next_interval(min_height)
pil_img = Image.new('RGB', (exp_width, exp_height), (255, 255, 255))
paste_coords = paste_top_left_coords(min_width, min_height,
exp_width, exp_height)
pil_img.paste(rot_img, paste_coords)
else:
pil_img = pil_img.rotate(
angle=degrees,
resample=Image.BICUBIC,
fillcolor=(255, 255, 255)
)
return pil_img
def rotate_to_origin(pil_img, original_height, original_width, degrees):
rot_img = pil_img.rotate(
angle=degrees,
resample=Image.BICUBIC,
expand=1,
fillcolor=(255, 255, 255)
)
rot_width, rot_height = rot_img.size
return rot_img.crop((
(rot_width - original_width) / 2,
(rot_height - original_height) / 2,
(rot_width - original_width) / 2 + original_width,
(rot_height - original_height) / 2 + original_height
))