forked from Brych2011/imgEvolving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathefficiency_tests.py
executable file
·63 lines (52 loc) · 1.66 KB
/
efficiency_tests.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
import pygame
from PIL import Image, ImageDraw
import numpy as np
import random
import time
IMAGE = Image.open('mona_lisa_small.jpg')
TARGET = np.array(IMAGE)
SIZE = IMAGE.size
IMAGE.show()
IMAGE.thumbnail((90,60))
IMAGE.show()
SHAPE = TARGET.shape
CIRCLES = 50
ACCURACY = 8
def genome_to_array(genome):
pgim = pygame.Surface((SIZE[0], SIZE[1]), pygame.SRCALPHA)
for circle in genome:
new_im = pygame.Surface((circle[2] * 2, circle[2] * 2), pygame.SRCALPHA)
new_im.fill(circle[0])
pgim.blit(new_im, circle[1])
return pygame.surfarray.array3d(pgim)
def pil_genome_to_array(genome):
result = Image.new('RGBA', SIZE)
for circle in genome:
new_circle = Image.new('RGBA', SIZE)
draw = ImageDraw.Draw(new_circle)
coordinates = circle[1]
radius = circle[2]
draw.ellipse((coordinates[0] - radius, coordinates[1] - radius,
coordinates[0] + radius, coordinates[1] + radius), tuple(circle[0]))
result = Image.alpha_composite(result, new_circle)
del new_circle
del draw
return np.array(result)
def random_genome():
result = []
for i in range(CIRCLES):
pos = [random.randint(0, SIZE[0]-1), random.randint(0, SIZE[1]-1)]
color = [random.randint(0, 255) for i in range(4)]
radius = random.randint(1, 75)
result.append([color, pos, radius])
return result
creature = random_genome()
t1 = time.time()
for i in range(5000):
arr_genome = genome_to_array(creature)
t_pygame = time.time() - t1
t1 = time.time()
for i in range(10):
arr_genome = pil_genome_to_array(creature)
t_pil = time.time() - t1
print(t_pil, t_pygame)