-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindividual.py
59 lines (45 loc) · 1.47 KB
/
individual.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
from triangle import Triangle
from PIL import Image, ImageDraw
class Individual:
def __init_(self):
self._id = 0
self._triangles = []
self._fitness = 0
self._image = None
@property
def id(self):
return self._id
@id.setter
def id(self, value):
self._id = value
@property
def triangles(self):
return self._triangles
@triangles.setter
def triangles(self, value):
self._triangles = value
@property
def fitness(self):
return self._fitness
@fitness.setter
def fitness(self, value):
self._fitness = value
@property
def image(self):
return self._image
@image.setter
def image(self, value):
self._image = value
def create_image(self, img_width, img_height):
# Create image object with the same dimensions as the original
# image.
img = Image.new('RGB', (img_width, img_height))
# Create draw object with alpha channel (so that image can be translucent).
draw = ImageDraw.Draw(img, 'RGBA')
for triangle in self.triangles:
# Draw each triangle in the list of genes.
draw.polygon([(triangle.a[0], triangle.a[1]), (triangle.b[0], triangle.b[1]),
(triangle.c[0], triangle.c[1])],
(triangle.color[0], triangle.color[1], triangle.color[2], 100))
# Set image drawn to the individual.
self.image = img