-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizer.py
51 lines (43 loc) · 1.59 KB
/
visualizer.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
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import sys
import os
def visualize(f, title: str = None):
#Transforms the input into a grid for example, [["FC","VC"],["VC","FC"]]
grid = [line.strip().split("\t") for line in f]
# Assuming the images are in images directory and named 'FC.png', 'VC.png', etc.
path_to_images = 'images/'
fig, axs = plt.subplots(len(grid), len(grid[0]), figsize=(5, 5))
if title != None:
fig.canvas.manager.set_window_title(title)
plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
for ax in axs.flatten():
ax.axis('off')
for i, row in enumerate(grid):
for j, img_code in enumerate(row):
img_path = f"{path_to_images}{img_code}.png"
img = mpimg.imread(img_path)
axs[i, j].imshow(img)
return fig
def render():
plt.show()
def usage():
script_name = sys.argv[0]
print(f"Usage:")
print(f"\tpython {script_name} [files...]")
if __name__ == "__main__":
if len(sys.argv) > 1:
for file in sys.argv[1:]:
try:
f = open(file)
except FileNotFoundError as e:
print(f"{e}", file=sys.stderr)
exit(1)
for file in sys.argv[1:]:
with open(file) as f:
visualize(f, os.path.basename(file))
plt.savefig("./diagrams/" + os.path.basename(file).replace(".out", ".out.png"))
# plt.savefig("./diagrams/" + os.path.basename(file).replace(".txt", ".png"))
else:
visualize(sys.stdin)
render()