Skip to content

Commit 83f5f2b

Browse files
committed
Fixed schematics not generating, refactored code
1 parent bddb049 commit 83f5f2b

1 file changed

Lines changed: 26 additions & 33 deletions

File tree

main.py

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def convert(self, path: str, output_path: str, show_progress: bool = True) -> No
8989
elif output_path.endswith(".schem"):
9090
with Image.open(path, "r") as img:
9191
generate_schematic.create_2d_schematic(
92-
self.get_blocks_2d_matirx(img, show_progress=show_progress),
92+
self.get_blocks_2d_matrix(img, show_progress=show_progress),
9393
output_path)
9494

9595
else:
@@ -107,29 +107,29 @@ def preprocess_image(self, image: Image) -> Image:
107107

108108
return cropped_image
109109

110-
def get_blocks_2d_matirx(self, image: Image, show_progress: bool = False) -> List[List[str]]:
110+
def get_blocks_2d_matrix(self, image: Image, show_progress: bool = False, chunk_size: int = 16) -> List[List[str]]:
111111
'''Returns a matrix of strings containing block names.'''
112112
preprocessed_image = self.preprocess_image(image)
113113
width, height = preprocessed_image.size
114-
chunks_x = width // 16
115-
chunks_y = height // 16
114+
chunks_x = width // chunk_size
115+
chunks_y = height // chunk_size
116+
117+
blocks_matrix = [[None] * chunks_y for _ in range(chunks_x)]
118+
total_iterations = chunks_x * chunks_y
116119

117-
total_iterations = chunks_x*chunks_y
118120
# Create a progress bar
119121
progress_bar = tqdm(total=total_iterations, disable=not show_progress)
120122

121-
blocks_matrix = list()
122123
for x in range(chunks_x):
123-
blocks_matrix.append(list())
124124
for y in range(chunks_y):
125-
left = x * 16
126-
upper = y * 16
127-
right = left + 16
128-
lower = upper + 16
125+
left = x * chunk_size
126+
upper = y * chunk_size
127+
right = left + chunk_size
128+
lower = upper + chunk_size
129129
chunk = preprocessed_image.crop((left, upper, right, lower))
130130

131131
closest_block = self.method(chunk)
132-
blocks_matrix[-1].append(closest_block[0])
132+
blocks_matrix[x][y] = closest_block
133133

134134
progress_bar.update(1)
135135

@@ -138,36 +138,29 @@ def get_blocks_2d_matirx(self, image: Image, show_progress: bool = False) -> Lis
138138
return blocks_matrix
139139

140140
def convert_image(self, image: Image, show_progress: bool = False) -> Image:
141-
# TODO: Use get_blocks_2d_matirx to not repeat the code
142-
preprocessed_image = self.preprocess_image(image)
141+
blocks_matrix = self.get_blocks_2d_matrix(image, show_progress)
142+
matrix_width = len(blocks_matrix)
143+
matrix_height = len(blocks_matrix[0])
144+
new_image = Image.new("RGB", size=(matrix_width * 16, matrix_height * 16))
143145

144-
width, height = preprocessed_image.size
145-
chunks_x = width // 16
146-
chunks_y = height // 16
146+
total_iterations = matrix_width * matrix_height
147147

148-
total_iterations = chunks_x * chunks_y
149148
# Create a progress bar
150149
progress_bar = tqdm(total=total_iterations, disable=not show_progress)
151150

152-
for x in range(chunks_x):
153-
for y in range(chunks_y):
154-
left = x * 16
155-
upper = y * 16
156-
right = left + 16
157-
lower = upper + 16
158-
chunk = preprocessed_image.crop((left, upper, right, lower))
151+
for x, row in enumerate(blocks_matrix):
152+
for y, block_name in enumerate(row):
153+
block_info = self.blocks.get(block_name)
154+
block_x, block_y = block_info["x"], block_info["y"]
155+
block_image = self.blocks_image.crop((block_x, block_y, block_x + 16, block_y + 16))
156+
new_image.paste(block_image, (x * 16, y * 16))
159157

160-
closest_block = self.method(chunk)
161-
preprocessed_image.paste(
162-
self.blocks_image.crop([self.blocks[closest_block]["x"],
163-
self.blocks[closest_block]["y"], self.blocks[closest_block]["x"]+16,
164-
self.blocks[closest_block]["y"]+16]), [left,upper,right,lower]
165-
)
166158
progress_bar.update(1)
167159

168160
# Close the progress bar
169161
progress_bar.close()
170-
return preprocessed_image
162+
163+
return new_image
171164

172165
def main():
173166
parser = argparse.ArgumentParser(description='Launch class arguments')
@@ -179,7 +172,7 @@ def main():
179172
# Add the optional arguments
180173
parser.add_argument('--filter', nargs='+', help='Filter options')
181174
parser.add_argument('--scale_factor', type=int, help='Scale factor', default=0)
182-
parser.add_argument('--compression_level', type=int, help='Compression level, greatly improves conversion speed, and loses some information along the way, do not set higher then 20, as it will cause very high memory consumption.', default=16)
175+
parser.add_argument('--compression_level', type=int, help='Compression level, greatly improves conversion speed, and loses some information along the way, do not set higher than 20, as it will cause very high memory consumption.', default=16)
183176
parser.add_argument('--method', type=str,
184177
choices=["abs_diff", "euclidean", "chebyshev_distance", "manhattan_distance", "cosine_similarity", "hamming_distance", "canberra_distance"], help='Method of finding the closest color to block', default="canberra_distance", required=False)
185178
parser.add_argument('--png_atlas_filename', type=str, default=resource_path('minecraft_textures_atlas_blocks.png_0.png'), help='PNG atlas filename')

0 commit comments

Comments
 (0)