Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
17 |
-
def
|
18 |
-
if
|
19 |
-
return
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
dungeon = {"Hallways": [], "Doors": [], "Rooms": []}
|
28 |
-
|
29 |
for img_file in image_files:
|
30 |
img_path = os.path.join(map_dir, img_file)
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
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 = "
|
84 |
|
85 |
# Scan the directory for .png files
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
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("
|
111 |
-
else:
|
112 |
-
st.write("No PNG files found in the specified directory.")
|
113 |
else:
|
114 |
-
st.write("
|
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")
|