awacke1 commited on
Commit
bec86a9
·
verified ·
1 Parent(s): 389d620

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -28
app.py CHANGED
@@ -1,7 +1,9 @@
1
  import streamlit as st
2
  import os
3
- from PIL import Image
4
  import random
 
 
 
5
 
6
  # Function to calculate aspect ratio
7
  def calculate_aspect_ratio(image_path):
@@ -36,20 +38,43 @@ def generate_dungeon_map(image_files):
36
  else:
37
  dungeon["Rooms"].append(img_file)
38
 
39
- # Add logic to connect rooms, hallways, and doors
40
- layout = {}
41
- if dungeon["Hallways"]:
42
- for i, hallway in enumerate(dungeon["Hallways"]):
43
- layout[f"Hallway {i+1}"] = {"type": "Hallway", "connected_to": []}
44
- if dungeon["Rooms"]:
45
- room = dungeon["Rooms"].pop(0)
46
- layout[f"Hallway {i+1}"]["connected_to"].append(room)
47
- if dungeon["Doors"]:
48
- door = dungeon["Doors"].pop(0)
49
- layout[f"Hallway {i+1}"]["connected_to"].append(door)
 
 
50
 
51
  return layout, dungeon
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  # Streamlit App
54
  st.title("Dynamic Dungeon Map Generator")
55
  st.write("Automatically generates dungeon maps by analyzing PNG images in a directory.")
@@ -62,28 +87,33 @@ if os.path.exists(map_dir):
62
  image_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
63
 
64
  if image_files:
65
- st.subheader("Classified Rooms")
66
- layout, dungeon = generate_dungeon_map(image_files)
67
 
68
- st.write("### Hallways")
69
- st.write(", ".join(dungeon["Hallways"]) if dungeon["Hallways"] else "No hallways found.")
 
 
70
 
71
- st.write("### Doors")
72
- st.write(", ".join(dungeon["Doors"]) if dungeon["Doors"] else "No doors found.")
73
 
74
- st.write("### Rooms")
75
- st.write(", ".join(dungeon["Rooms"]) if dungeon["Rooms"] else "No rooms found.")
 
 
76
 
77
- st.subheader("Dungeon Layout")
78
- for key, value in layout.items():
79
- st.write(f"{key}: {value}")
80
 
 
 
 
 
81
  else:
82
  st.write("No PNG files found in the specified directory.")
83
  else:
84
  st.write("The specified directory does not exist. Please check the path.")
85
 
86
- # File upload option
87
  st.sidebar.title("Options")
88
  uploaded_file = st.sidebar.file_uploader("Upload a PNG file", type="png")
89
 
@@ -92,7 +122,3 @@ if uploaded_file:
92
  with open(os.path.join(map_dir, uploaded_file.name), "wb") as f:
93
  f.write(uploaded_file.getbuffer())
94
  st.sidebar.success("File uploaded successfully! Refresh the app to include it in the dungeon map.")
95
-
96
- # Placeholder for visualization
97
- st.subheader("Visual Representation")
98
- st.write("A feature to visualize the map will be added soon.")
 
1
  import streamlit as st
2
  import os
 
3
  import random
4
+ from PIL import Image
5
+ import networkx as nx
6
+ import matplotlib.pyplot as plt
7
 
8
  # Function to calculate aspect ratio
9
  def calculate_aspect_ratio(image_path):
 
38
  else:
39
  dungeon["Rooms"].append(img_file)
40
 
41
+ # Generate connections between elements
42
+ layout = nx.Graph()
43
+ for i, hallway in enumerate(dungeon["Hallways"]):
44
+ layout.add_node(f"Hallway {i+1}", type="Hallway")
45
+ if dungeon["Rooms"]:
46
+ room = dungeon["Rooms"].pop(0)
47
+ layout.add_node(room, type="Room")
48
+ layout.add_edge(f"Hallway {i+1}", room)
49
+
50
+ if dungeon["Doors"]:
51
+ door = dungeon["Doors"].pop(0)
52
+ layout.add_node(door, type="Door")
53
+ layout.add_edge(f"Hallway {i+1}", door)
54
 
55
  return layout, dungeon
56
 
57
+ # Function to visualize the dungeon layout
58
+ def visualize_dungeon(layout, output_path="dungeon_tree.png"):
59
+ plt.figure(figsize=(12, 8))
60
+ pos = nx.spring_layout(layout) # Positions for nodes
61
+ types = nx.get_node_attributes(layout, 'type')
62
+
63
+ # Draw nodes with different colors
64
+ color_map = {
65
+ "Hallway": "blue",
66
+ "Door": "green",
67
+ "Room": "orange"
68
+ }
69
+ colors = [color_map[types[node]] for node in layout.nodes]
70
+
71
+ nx.draw(
72
+ layout, pos, with_labels=True, node_color=colors, node_size=2000,
73
+ font_size=10, font_color="white", font_weight="bold", edge_color="gray"
74
+ )
75
+ plt.savefig(output_path)
76
+ plt.close()
77
+
78
  # Streamlit App
79
  st.title("Dynamic Dungeon Map Generator")
80
  st.write("Automatically generates dungeon maps by analyzing PNG images in a directory.")
 
87
  image_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
88
 
89
  if image_files:
90
+ st.subheader("Generate Dungeon Map")
 
91
 
92
+ # Add emoji buttons
93
+ if st.button("🔄 Regenerate Dungeon"):
94
+ layout, dungeon = generate_dungeon_map(image_files)
95
+ visualize_dungeon(layout)
96
 
97
+ st.image("dungeon_tree.png", caption="Dungeon Layout Tree")
 
98
 
99
+ # Display classified rooms
100
+ st.subheader("Classified Rooms")
101
+ st.write("### Hallways")
102
+ st.write(", ".join(dungeon["Hallways"]) if dungeon["Hallways"] else "No hallways found.")
103
 
104
+ st.write("### Doors")
105
+ st.write(", ".join(dungeon["Doors"]) if dungeon["Doors"] else "No doors found.")
 
106
 
107
+ st.write("### Rooms")
108
+ st.write(", ".join(dungeon["Rooms"]) if dungeon["Rooms"] else "No rooms found.")
109
+ else:
110
+ st.write("Click the button above to generate a dungeon map.")
111
  else:
112
  st.write("No PNG files found in the specified directory.")
113
  else:
114
  st.write("The specified directory does not exist. Please check the path.")
115
 
116
+ # Sidebar for file upload
117
  st.sidebar.title("Options")
118
  uploaded_file = st.sidebar.file_uploader("Upload a PNG file", type="png")
119
 
 
122
  with open(os.path.join(map_dir, uploaded_file.name), "wb") as f:
123
  f.write(uploaded_file.getbuffer())
124
  st.sidebar.success("File uploaded successfully! Refresh the app to include it in the dungeon map.")