-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_convert_txt_to_png.py
55 lines (45 loc) · 1.87 KB
/
04_convert_txt_to_png.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
# -*- coding: utf-8 -*-
"""04_convert_txt_to_png.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1xY_OCBQqT-9u8zmhnDEJdf0s7stHwv3H
"""
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
tiles_dir = './tiles'
chars2pngs = {
"-": Image.open(f'./tiles/smb-background.png'),
"X": Image.open(f'./tiles/smb-unpassable.png'),
"S": Image.open(f'./tiles/smb-breakable.png'),
"?": Image.open(f'./tiles/smb-question.png'),
"Q": Image.open(f'./tiles/smb-question.png'),
"o": Image.open(f'./tiles/smb-coin.png'),
"E": Image.open(f'./tiles/smb-enemy.png'),
"<": Image.open(f'./tiles/smb-tube-top-left.png'),
">": Image.open(f'./tiles/smb-tube-top-right.png'),
"[": Image.open(f'./tiles/smb-tube-lower-left.png'),
"]": Image.open(f'./tiles/smb-tube-lower-right.png'),
"x": Image.open(f'./tiles/smb-path.png'), # self-created
"Y": Image.open(f'./tiles/Y.png'), # self-created
"N": Image.open(f'./tiles/N.png'), # self-created
"B": Image.open(f'./tiles/cannon_top.png'),
"b": Image.open(f'./tiles/cannon_bottom.png'),
}
def char_array_to_image(array):
"""
Convert a 16-by-16 array of integers into a PIL.Image object
param: array: a 16-by-16 array of integers
"""
image = Image.new('RGB',(array.shape[1] * 16, array.shape[0] * 16))
for row, seg in enumerate(array):
for col, char in enumerate(seg):
image.paste(chars2pngs[char], (col * 16, row * 16))
return image
for i in range(10):
with open(f'./generated_levels_txt/{i+1}.txt', 'r') as txt_f:
infile = np.array([list(line.rstrip()) for line in txt_f.readlines()])
plt.figure(figsize=(20, 4))
plt.imshow(char_array_to_image(infile))
plt.axis('off')
plt.savefig(f'./generated_levels_png/{i}.png', dpi=200, bbox_inches='tight')