awacke1 commited on
Commit
377a3b2
1 Parent(s): d74f0a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -91
app.py CHANGED
@@ -1,9 +1,7 @@
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):
@@ -13,105 +11,75 @@ def calculate_aspect_ratio(image_path):
13
  divisor = gcd(width, height)
14
  return width // divisor, height // divisor
15
 
16
- # Function to classify room types based on aspect ratio
17
- def classify_room(aspect_ratio):
18
- if aspect_ratio == (16, 9) or aspect_ratio == (19, 6):
19
- return "Hallway"
20
- elif aspect_ratio[0] < 8 and aspect_ratio[1] < 8:
21
- return "Door"
22
- else:
23
- return "Room"
24
-
25
- # Function to generate a layout
26
- def generate_dungeon_map(image_files):
27
- dungeon = {"Hallways": [], "Doors": [], "Rooms": []}
28
-
29
  for img_file in image_files:
30
  img_path = os.path.join(map_dir, img_file)
31
- aspect_ratio = calculate_aspect_ratio(img_path)
32
- room_type = classify_room(aspect_ratio)
33
-
34
- if room_type == "Hallway":
35
- dungeon["Hallways"].append(img_file)
36
- elif room_type == "Door":
37
- dungeon["Doors"].append(img_file)
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.")
81
 
82
  # Directory for images
83
- map_dir = "maps" # Replace with the actual directory path
84
 
85
  # Scan the directory for .png files
86
- if os.path.exists(map_dir):
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")
 
1
  import streamlit as st
2
  import os
3
+ from PIL import Image, ImageDraw
4
  import random
 
 
 
5
 
6
  # Function to calculate aspect ratio
7
  def calculate_aspect_ratio(image_path):
 
11
  divisor = gcd(width, height)
12
  return width // divisor, height // divisor
13
 
14
+ # Function to randomly position images while ensuring no overlap
15
+ def arrange_images(image_files, output_path="dungeon_layout.png"):
16
+ if not image_files:
17
+ return None
18
+
19
+ # Initialize variables for layout
20
+ positions = [] # Keeps track of image positions
21
+ canvas_size = (3000, 3000) # Adjust based on your needs
22
+ canvas = Image.new("RGBA", canvas_size, "white")
23
+ draw = ImageDraw.Draw(canvas)
24
+
 
 
25
  for img_file in image_files:
26
  img_path = os.path.join(map_dir, img_file)
27
+ with Image.open(img_path) as img:
28
+ width, height = img.size
29
+
30
+ # Randomly decide horizontal or vertical placement
31
+ if random.choice(["horizontal", "vertical"]) == "horizontal":
32
+ x_center = random.randint(width // 2, canvas_size[0] - width // 2)
33
+ y_center = canvas_size[1] // 2
34
+ else:
35
+ x_center = canvas_size[0] // 2
36
+ y_center = random.randint(height // 2, canvas_size[1] - height // 2)
37
+
38
+ # Calculate position
39
+ x1 = x_center - width // 2
40
+ y1 = y_center - height // 2
41
+ x2 = x1 + width
42
+ y2 = y1 + height
43
+
44
+ # Ensure no overlap with existing images
45
+ overlap = any(
46
+ x1 < pos[2] and x2 > pos[0] and y1 < pos[3] and y2 > pos[1]
47
+ for pos in positions
48
+ )
49
+
50
+ if not overlap:
51
+ positions.append((x1, y1, x2, y2))
52
+ canvas.paste(img, (x1, y1))
53
+
54
+ # Draw connection line
55
+ if len(positions) > 1:
56
+ prev_x, prev_y = (positions[-2][0] + positions[-2][2]) // 2, (positions[-2][1] + positions[-2][3]) // 2
57
+ curr_x, curr_y = (x1 + x2) // 2, (y1 + y2) // 2
58
+ draw.line([(prev_x, prev_y), (curr_x, curr_y)], fill="black", width=5)
59
+
60
+ canvas.save(output_path)
61
+ return output_path
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  # Streamlit App
64
  st.title("Dynamic Dungeon Map Generator")
65
+ st.write("Automatically generates dungeon maps by analyzing PNG images in a top-level directory.")
66
 
67
  # Directory for images
68
+ map_dir = "." # Top-level directory
69
 
70
  # Scan the directory for .png files
71
+ image_files = [f for f in os.listdir(map_dir) if f.endswith(".png")]
72
+
73
+ if image_files:
74
+ # Add "Create Map" button
75
+ if st.button("🗺️ Create Map"):
76
+ layout_image = arrange_images(image_files)
77
+ if layout_image:
78
+ st.image(layout_image, caption="Generated Dungeon Map Layout")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  else:
80
+ st.write("Failed to generate a map. Please check the images in the directory.")
 
 
81
  else:
82
+ st.write("No PNG files found in the top-level directory.")
83
 
84
  # Sidebar for file upload
85
  st.sidebar.title("Options")