forked from AunSiro/wavefunction-mapmaking
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
175 lines (155 loc) · 5.24 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
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 30 18:30:01 2024
@author: Mapmaking Team PyCamp Spain 2024
"""
import os, sys
from PIL import Image
from os import listdir
from os.path import isfile, join
class Tile:
def __init__(
self,
image_list,
frontier_list,
special = '',
rotable = True,
symmetric = False,
):
self.image_list = image_list
self.frontier_list = frontier_list
self.rotable = rotable
self.symmetric = symmetric
self.special = special
def is_compatible_with(self, other_tile, other_position = 'up'):
if other_position == 'up':
my_frontier = self.frontier_list[0]
their_frontier = other_tile.frontier_list[2]
elif other_position == 'down':
my_frontier = self.frontier_list[2]
their_frontier = other_tile.frontier_list[0]
elif other_position == 'right':
my_frontier = self.frontier_list[1]
their_frontier = other_tile.frontier_list[3]
elif other_position == 'left':
my_frontier = self.frontier_list[3]
their_frontier = other_tile.frontier_list[1]
else:
raise ValueError(f'invalid value for other position: {other_position}')
return my_frontier == their_frontier[::-1]
def frontiers_from_name(fname):
fname = fname.partition('.')[0]
frontiers = []
for ii in range(3):
_f, _, fname = fname.partition('-')
frontiers.append(_f)
if '-' in fname:
_f, _, special = fname.partition('-')
frontiers.append(_f)
else:
frontiers.append(fname)
special = ''
return frontiers, special
def rotate_frontier_list(old_frontier_list):
new_frontier_list = [_front for _front in old_frontier_list[1:]]
new_frontier_list.append(old_frontier_list[0])
return new_frontier_list
def is_simmetric(frontier_list):
base_string = '-'.join(frontier_list)
inverse_list = [_f[::-1] for _f in frontier_list[::-1]]
#print(base_string)
for ii in range(4):
inverse_string = '-'.join(inverse_list)
#print(inverse_string)
if inverse_string == base_string:
return True
inverse_list = rotate_frontier_list(inverse_list)
return False
def tile_from_file(fname, pathname):
frontiers, special = frontiers_from_name(fname)
fpath = join(pathname, fname)
im = Image.open(fpath)
if special == '':
rotable = True
else:
rotable = False
newtile = Tile(
image_list = [im,],
frontier_list = frontiers,
special = special,
rotable = rotable,
symmetric = False,
)
return newtile
def rotate_tile(oldtile):
old_im_list = oldtile.image_list
old_frontier_list = oldtile.frontier_list
new_im_list = [_im.rotate(-90) for _im in old_im_list]
new_frontier_list = [old_frontier_list[-1]] + [_front for _front in old_frontier_list[:-1]]
newtile = Tile(
new_im_list,
new_frontier_list,
special = oldtile.special,
rotable = oldtile.rotable,
symmetric = oldtile.symmetric,
)
return newtile
def flip_tile(oldtile):
old_im_list = oldtile.image_list
old_frontier_list = oldtile.frontier_list
new_im_list = [_im.transpose(Image.Transpose.FLIP_LEFT_RIGHT) for _im in old_im_list]
new_frontier_list = [_front[::-1] for _front in old_frontier_list]
new_frontier_list[1] = old_frontier_list[3][::-1]
new_frontier_list[3] = old_frontier_list[1][::-1]
newtile = Tile(
new_im_list,
new_frontier_list,
special = oldtile.special,
rotable = oldtile.rotable,
symmetric = oldtile.symmetric,
)
return newtile
def create_tile_list(pathname):
file_list = [f for f in listdir(pathname) if isfile(join(pathname, f))]
tile_list = []
for file in file_list:
new_tile = tile_from_file(file, pathname)
tile_list.append(new_tile)
if new_tile.rotable == True:
for ii in range(3):
new_tile = rotate_tile(new_tile)
tile_list.append(new_tile)
if not is_simmetric(new_tile.frontier_list):
new_tile = flip_tile(new_tile)
tile_list.append(new_tile)
if new_tile.rotable == True:
for ii in range(3):
new_tile = rotate_tile(new_tile)
tile_list.append(new_tile)
return tile_list
def generate_final_image(image_names: list[list], width: int, height: int):
final_image = Image.new('RGB', (width * 64, height * 64))
for x, image_name_row in enumerate(image_names):
for y, image_name in enumerate(image_name_row):
image = Image.open(f'tiles/{image_name}')
final_image.paste(image, (y * 64, x * 64))
final_image.save('final_image.png', 'JPEG')
if __name__ == '__main__':
image_names = [
[
'a-a-a-a.jpg',
'a-a-a-a-ballena.jpg',
'a-a-a-a-barco.jpg',
],
[
'a-t-a-t-canal.jpg',
'a-t-a-t-istmo.JPG',
't-a-a-a.jpg',
],
[
't-t-a-a.jpg',
't-t-t-a.jpg',
't-t-t-t.jpg',
],
]
generate_final_image(image_names, 3, 3)