-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgiwp.py
146 lines (115 loc) · 4.6 KB
/
giwp.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
134
135
136
137
138
139
140
141
142
143
144
145
146
from PIL import Image, ImageDraw
import matplotlib.path as mplPath
import numpy as np
import webbrowser
import os.path
import copy
import sys
import random
import argparse
DIMENSION = 30
N = 1000
def fitness(img, points):
a = points[0]
b = points[1]
c = points[2]
maxY = max(a[1], b[1], c[1])
maxX = max(a[0], b[0], c[0])
minY = min(a[1], b[1], c[1])
minX = min(a[0], b[0], c[0])
minY = max(minY, 0)
minX = max(minX, 0)
maxY = min(maxY, img.size[1])
maxX = min(maxX, img.size[0])
bbPath = mplPath.Path(np.array(points))
count = 0
RGB_img = np.zeros(3)
rgb_img = img.convert('RGBA')
for y in range(minY, maxY):
for x in range(minX, maxX):
if (bbPath.contains_point((x, y))):
rgb_img_tmp = rgb_img.getpixel((x, y))
RGB_img[0] += rgb_img_tmp[0]
RGB_img[1] += rgb_img_tmp[1]
RGB_img[2] += rgb_img_tmp[2]
count += 1
if (count <= 0):
return np.array([256, 256, 256])
fitness_draw = np.array([int(RGB_img[0] / count),
int(RGB_img[1] / count),
int(RGB_img[2] / count)])
return fitness_draw
def generate_point(size):
point = np.array([[-1, -1], [-1, -1], [-1, -1]])
while (point[0][0] < 0 or point[0][1] < 0 or point[1][0] < 0 or point[1][1] < 0 or point[2][0] < 0 or point[2][1] < 0):
point[2] = list([random.randrange(0, size[0]), random.randrange(0, size[1])])
rand = random.random()
if (rand <= 0.25):
point[0] = [point[2][0] - DIMENSION / 2,
random.randrange(point[2][1], point[2][1] + DIMENSION)]
point[1] = [point[2][0] + DIMENSION / 2,
random.randrange(point[2][1], point[2][1] + DIMENSION)]
elif (rand <= 0.50):
point[0] = [random.randrange(
point[2][0], point[2][0] + DIMENSION), point[2][1] - DIMENSION / 2]
point[1] = [random.randrange(
point[2][0], point[2][0] + DIMENSION), point[2][1] + DIMENSION / 2]
elif (rand <= 0.75):
point[0] = [point[2][0] - DIMENSION / 2,
random.randrange(point[2][1] - DIMENSION, point[2][1])]
point[1] = [point[2][0] + DIMENSION / 2,
random.randrange(point[2][1] - DIMENSION, point[2][1])]
elif (rand <= 1):
point[0] = [random.randrange(
point[2][0] - DIMENSION, point[2][0]), point[2][1] - DIMENSION / 2]
point[1] = [random.randrange(
point[2][0] - DIMENSION, point[2][0]), point[2][1] + DIMENSION / 2]
return point
def my_draw(img, color, points, fitness):
size = img.size
img2 = Image.new('RGBA', size)
draw = Image.new('RGBA', size)
pdraw = ImageDraw.Draw(draw)
array_point = []
for point in points:
array_point.append(tuple([point[0], point[1]]))
pdraw.polygon(array_point, fill=(color[0], color[1], color[2], fitness))
img2 = Image.alpha_composite(img2, draw)
return img2
def generate_img(img):
polygons = []
parent = Image.new('RGBA', img.size)
for i in range(N):
points = generate_point(img.size)
color = fitness(img, points)
polygons.append((color, points))
# parent = Image.alpha_composite(parent, draw_polygon)
# parent.save("pic.png", 'PNG')
for polygon in polygons:
draw_polygon = my_draw(img, polygon[0], polygon[1], 100)
parent = Image.alpha_composite(parent, draw_polygon)
parent.save("pic.png", 'PNG')
def main(argv):
path = os.getcwd()
if not os.path.exists(path + "/file.html"):
print("\nPut the \".html\" here: " + path)
sys.exit()
parser = argparse.ArgumentParser()
parser.add_argument('--number-polygons', type=int, default=1000, metavar='N',
help='number of polygons to use (default: 1000)')
parser.add_argument('--img', type=str, default="Image/image.png",
help='the image to use for the generation of polygons')
parser.add_argument('--dimension', type=int, default=30, metavar='N',
help='size of a single polygon (default: 30)')
args = parser.parse_args()
global N, DIMENSION
N = args.number_polygons
DIMENSION = args.dimension
# open image
img = Image.open(path + "/" + args.img)
# generate the image with polygons
generate_img(img)
# open a web page with the image that refresh every second
webbrowser.open(path + "/file.html")
if __name__ == "__main__":
main(sys.argv)