-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
197 lines (142 loc) · 5.78 KB
/
utils.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
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
193
194
195
196
197
from PIL import Image
from Circle import *
import math
from random import randint
from tqdm import tqdm
from joblib import Parallel, delayed
def isHexa(hexa):
if len(hexa)!= 6 and len(hexa)!= 7:
return False
if len(hexa) == 7 and hexa[0] != '#':
return False
if len(hexa)==7:
hexa = hexa[1:]
possibleChar = 'aAbBcCdDeEfF0123456789'
for i in hexa:
if i not in possibleChar:
return False
return True
def normalizeMask(mask):
temp = max(mask)
return [mask[0]/temp, mask[1]/temp, mask[2]/temp]
def saveImg(img, name=None):
nameToSave = input("Choose the file name to save the image, including the extension : ") if name is None else name
img = img.save("samples/"+nameToSave)
def createUniformImageWithHexa(hexa, largeur, hauteur):
if not isHexa(hexa):
print("Wrong hexadecimal format.")
return isHexa(hexa)
if len(hexa)==7:
hexa = hexa[1:]
hexaInInt = list(map(lambda x : int(x, 16), [hexa[0:2],hexa[2:4], hexa[4:]]))
img = Image.new('RGB', (largeur,hauteur), color = (int(hexaInInt[0]),int(hexaInInt[1]),int(hexaInInt[2])))
return img
def is_point_in_circle(i, j, circle : Circle):
distance = math.sqrt((i - circle.x)**2 + (j - circle.y)**2)
return distance <= circle.radius
def filterNegative(img):
(largeur,hauteur)=img.size
newImg = Image.new('RGB', (largeur,hauteur), color = (0, 0, 0))
for i in range (largeur):
for j in range (hauteur):
p=img.getpixel((i,j))
newImg.putpixel((i,j),((255-p[0]),(255-p[1]),(255-p[2])))
return newImg
def addCircleInMatrix(circle, matrix, opacity=128):
(largeur,hauteur)=len(matrix), len(matrix[0])
for i in range (circle.x - circle.radius - 1, min(circle.x + circle.radius + 1, largeur)):
for j in range (circle.y - circle.radius - 1, min(circle.y + circle.radius + 1, hauteur)):
if is_point_in_circle(i,j,circle):
p=matrix[i][j]
new_color = (
min(255, (circle.color[0]) + p[0]//circle.z),
min(255, (circle.color[1]) + p[1]//circle.z),
min(255, (circle.color[2]) + p[2]//circle.z),
)
blended_color = (
(p[0] * (255 - opacity) + new_color[0] * opacity) // 255,
(p[1] * (255 - opacity) + new_color[1] * opacity) // 255,
(p[2] * (255 - opacity) + new_color[2] * opacity) // 255,
)
matrix[i][j] = blended_color
return matrix
def getMatrixFromImage(img):
matrixPixel = []
(largeur,hauteur)=img.size
for i in range (largeur):
line = []
for j in range (hauteur):
line.append(img.getpixel((i,j)))
matrixPixel.append(line)
return matrixPixel
def getImageFromMatrix(matrix):
largeur, hauteur = len(matrix), len(matrix[0])
img = Image.new('RGB', (largeur,hauteur), color = (0, 0, 0))
for i in range (largeur):
for j in range (hauteur):
p=matrix[i][j]
img.putpixel((i,j),(p[0],p[1],p[2]))
return img
def addLayerOfCircles(matrix, nbCircle, numLayer, para=False):
newMatrix = deepCopyMatrix(matrix)
(largeur,hauteur)=len(newMatrix), len(newMatrix[0])
maxRadius = (largeur+hauteur)//2 // 10
minRadius = (largeur+hauteur)//2 // 20
circles = []
for _ in range (nbCircle):
color = [randint(0, 255),randint(0, 255),randint(0, 255)]
circle = Circle(
x=randint(0,largeur),
y=randint(0,hauteur),
z=numLayer+1,
radius=randint(minRadius, maxRadius),
color=color)
circles.append(circle)
if not para :
progress_bar = tqdm(total=len(circles), desc="Processing")
for c in range(len(circles)):
newMatrix = addCircleInMatrix(circles[c], newMatrix)
progress_bar.update(1)
progress_bar.close()
else:
for c in range(len(circles)):
newMatrix = addCircleInMatrix(circles[c], newMatrix)
return newMatrix
def multipleLayers(largeur, hauteur, nbCircle, nbLayer, hexa="#FFFFFF"):
img = createUniformImageWithHexa(hexa, largeur, hauteur)
matrix = getMatrixFromImage(img)
for i in range(1, nbLayer+1):
matrix = addLayerOfCircles(matrix, nbCircle, i)
newImg = getImageFromMatrix(matrix)
return newImg
def applyMatrixOnMatrix(frontMatrix, backMatrix, z, opacity=12):
newMatrix = deepCopyMatrix(backMatrix)
(largeur,hauteur)=len(backMatrix), len(backMatrix[0])
for i in range (largeur):
for j in range (hauteur):
back=backMatrix[i][j]
front = frontMatrix[i][j]
new_color = (
min(255, ((front[0])*z + back[0]*(z-1)) //(2*z-1)),
min(255, ((front[1])*z + back[1]*(z-1)) //(2*z-1)),
min(255, ((front[2])*z + back[2]*(z-1)) //(2*z-1)),
)
newMatrix[i][j] = new_color
return newMatrix
def multipleLayersParallel(largeur, hauteur, nbCircle, nbLayer, z=2, hexa="#FFFFFF"):
img = createUniformImageWithHexa(hexa, largeur, hauteur)
firstMatrix = getMatrixFromImage(img)
allMatrix = Parallel(n_jobs=-1)(delayed(addLayerOfCircles)(firstMatrix, nbCircle, i, True) for i in range(1, nbLayer + 1))
newMatrix = deepCopyMatrix(firstMatrix)
for i in range(len(allMatrix)):
newMatrix = applyMatrixOnMatrix(allMatrix[i], newMatrix, 10)
newImg = getImageFromMatrix(newMatrix)
return newImg
def deepCopyMatrix(matrix):
newMatrix = []
for i in range(len(matrix)):
line = []
for j in range(len(matrix[i])):
line.append(matrix[i][j])
newMatrix.append(line)
return newMatrix