-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
235 lines (186 loc) · 7.74 KB
/
Main.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import os
import PIL
from tkinter import *
from Tile import *
from Grid import *
import numpy as np
from PIL import ImageTk
DEFAULT = 5 #the index of the default tile 5 without allDir, 6 otherwise
tileSize = 20
def getAllTiles():
""" Returns a list of all the tiles"""
# add a special Tile "all Directions"
# tilesName = [ "blank", "up", "right", "down", "left","allDir","default"];
tilesName = [ "blank", "up", "right", "down", "left","default"];
tiles = []
for i in range (len(tilesName)):
tiles.append(PIL.Image.open(os.path.dirname(os.path.abspath(__file__))+"/tiles/"+tilesName[i]+".png").resize((tileSize,tileSize)))
return tiles
def checkValid(array, validOptions):
# Parsing the array and removing the invalid options
lenghtArray = len(array)-1
for i in range(lenghtArray,-1,-1):
if array[i] not in validOptions:
array.pop(i)
def restart():
global grid, DIM, tiles, saveButton
# Reset the grid
grid = Grid(DIM, tiles)
saveButton.config(state=DISABLED)
def save():
global grid, tiles, DIM
# The image is a grid of tiles
img = np.zeros((DIM*tileSize,DIM*tileSize,3), dtype=np.uint8)
for i in range (DIM):
for j in range (DIM):
# Concatenate the tile with the image to obtain the final image
subTile = list(np.array(tiles[grid.grid[i][j].options[0]].image))
img[i*tileSize:(i+1)*tileSize,j*tileSize:(j+1)*tileSize,:] = subTile
img = PIL.Image.fromarray(img)
img.save("ImageResult.png")
def draw(grid):
global tileImages, gridLabel, tiles
for i in range (DIM):
for j in range (DIM):
cell = grid.grid[i][j]
if cell.collapsed:
try:
# For each cell, we draw the tile with the correct index in the tileImages list
index = cell.options[0]
except IndexError:
# As my version has no backtrack, something there is no possible option so we start from scratch
restart()
tileImg = tileImages[index]
gridLabel[i][j].config(image=tileImg)
else:
# If the cell is not collapsed, we draw the default tile
defaultTile = tileImages[DEFAULT]
gridLabel[i][j].config(image=defaultTile)
def update():
global grid, tiles, saveButton
draw(grid)
#looking for the cell with the least options
gridCopy = grid.copy(tiles)
cell = gridCopy.getCellLeastEntropy()
# If were done with the grid, we can save the image
if cell is None:
saveButton.config(state=ACTIVE)
else:
#collapse the cell
cell.collapsed = True
try:
cell.options = [random.choice(cell.options)]
except:
restart()
#update the grid
grid = gridCopy
# We create a copy of the grid to be the next iteration
nextGrid = Grid(DIM, tiles)
for i in range (DIM):
for j in range (DIM):
cell = grid.grid[i][j]
if cell.collapsed:
# if the cell is collapsed, we just copy the cell to the new grid
nextGrid.grid[i][j] = cell.copy()
# print("collapsed",nextGrid.grid[i][j])
else:
# If the cell is not collapsed, we look for the possible options
# We look in the 4 directions and we remove the options that are not possible
# to reduce the entropy of the cell
options = [i for i in range(len(tiles))]
# look up
if i > 0:
up = grid.grid[i-1][j]
validOptions = []
for option in up.options:
valid = tiles[option].down
validOptions.extend(valid)
checkValid(options, validOptions)
# look right
if j < DIM-1:
right = grid.grid[i][j+1]
validOptions = []
for option in right.options:
valid = tiles[option].left
validOptions.extend(valid)
checkValid(options, validOptions)
# look down
if i < DIM-1:
down = grid.grid[i+1][j]
validOptions = []
for option in down.options:
valid = tiles[option].up
validOptions.extend(valid)
checkValid(options, validOptions)
# look left
if j > 0:
left = grid.grid[i][j-1]
validOptions = []
for option in left.options:
valid = tiles[option].right
validOptions.extend(valid)
checkValid(options, validOptions)
nextGrid.grid[i][j] = Cell.createCellFromOptions(options)
grid = nextGrid
window.after(1,update)
def main():
global grid, window, DIM, tiles, tileImages, gridLabel, saveButton
# Dimension of the grid (number of cells DIM x DIM)
DIM = 15
window = Tk()
window.title("Wave function Collapse")
# if the dimension is under a certain value, we use a set window size
if DIM > 7:
window.geometry(f"{DIM*tileSize}x{DIM*tileSize+50}")
else:
window.geometry("200x200")
window.resizable(0, 0)
# Load and create all tiles
# This is really specific to the tiles you input in the tiles folder
tileImages = getAllTiles()
# Create the Tiles
# the code here can be used to create a grid of tiles, but end up bad for saved image a the end of the program
# tiles = [
# Tile(tileImages[0], [0,0,0,0]),
# Tile(tileImages[1], [1,1,0,1]),
# Tile(tileImages[1], [1,1,0,1]).rotate(1),
# Tile(tileImages[1], [1,1,0,1]).rotate(2),
# Tile(tileImages[1], [1,1,0,1]).rotate(3),
# # Tile(tileImages[5], [1,1,1,1]),
# ]
tiles = [
Tile(tileImages[0], [0,0,0,0]),
Tile(tileImages[1], [1,1,0,1]),
Tile(tileImages[2], [1,1,1,0]),
Tile(tileImages[3], [0,1,1,1]),
Tile(tileImages[4], [1,0,1,1]),
# Tile(tileImages[5], [1,1,1,1]),
]
# we retransform the Image objects to PhotoImage objects
for i in range (len(tileImages)):
tileImages[i] = ImageTk.PhotoImage(tileImages[i])
# Generate the adjacency rules based on the edges of the tiles
for tile in tiles:
tile.analyzeEdges(tiles)
# print(tile)
# Create a cell for each spot in the grid
grid = Grid(DIM, tiles)
gridLabel = []
for i in range (DIM):
gridLabel.append([])
for j in range (DIM):
label = Label(window, image=tileImages[5], borderwidth=0)
label.grid(row=i, column=j)
gridLabel[i].append(label)
# Restart button
restartButton = Button(window, text="Restart", command=restart)
restartButton.grid(row=DIM, column=0, columnspan=int(DIM/2), padx=10, pady=10)
# Save button
saveButton = Button(window, text="Save", command=save, state=DISABLED)
saveButton.grid(row=DIM, column=int(DIM/2), columnspan=int(DIM/2), padx=10, pady=10)
# Start the update loop
update()
# mainloop
window.mainloop()
if __name__ == "__main__":
main()